Skip to content

Commit

Permalink
added #titleTemplate, closes #2852
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed May 15, 2022
1 parent e87e065 commit 593a275
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
1 change: 0 additions & 1 deletion src/public/app/services/entrypoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export default class Entrypoints extends Component {
const inboxNote = await dateNoteService.getInboxNote();

const {note} = await server.post(`notes/${inboxNote.noteId}/children?target=into`, {
title: 'new note',
content: '',
type: 'text',
isProtected: inboxNote.isProtected && protectedSessionHolder.isProtectedSessionAvailable()
Expand Down
4 changes: 1 addition & 3 deletions src/public/app/services/note_create.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ async function createNote(parentNotePath, options = {}) {
[options.title, options.content] = parseSelectedHtml(window.cutToNote.getSelectedHtml());
}

const newNoteName = options.title || "new note";

const parentNoteId = treeService.getNoteIdFromNotePath(parentNotePath);

if (options.type === 'mermaid' && !options.content) {
Expand All @@ -41,7 +39,7 @@ async function createNote(parentNotePath, options = {}) {
}

const {note, branch} = await server.post(`notes/${parentNoteId}/children?target=${options.target}&targetBranchId=${options.targetBranchId || ""}`, {
title: newNoteName,
title: options.title,
content: options.content || "",
isProtected: options.isProtected,
type: options.type,
Expand Down
9 changes: 9 additions & 0 deletions src/public/app/widgets/attribute_widgets/attribute_detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ const ATTR_HELP = {
"shareDisallowRobotIndexing": `will forbid robot indexing of this note via <code>X-Robots-Tag: noindex</code> header`,
"displayRelations": "comma delimited names of relations which should be displayed. All other ones will be hidden.",
"hideRelations": "comma delimited names of relations which should be hidden. All other ones will be displayed.",
"titleTemplate": `default title of notes created as children of this note. The value is evaluated as JavaScript string
and thus can be enriched with dynamic content via the injected <code>now</code> and <code>parentNote</code> variables. Examples:
<ul>
<li><code>\${parentNote.getLabelValue('authorName')}'s literary works</code></li>
<li><code>Log for \${now.format('YYYY-MM-DD HH:mm:ss')}</code></li>
</ul>
See <a href="https://github.com/zadam/trilium/wiki/Default-note-title">wiki with details</a>, API docs for <a href="https://zadam.github.io/trilium/backend_api/Note.html">parentNote</a> and <a href="https://day.js.org/docs/en/display/format">now</a> for details.`
},
"relation": {
"runOnNoteCreation": "executes when note is created on backend",
Expand Down
1 change: 1 addition & 0 deletions src/services/builtin_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ module.exports = [
{ type: 'label', name: 'shareDisallowRobotIndexing' },
{ type: 'label', name: 'displayRelations' },
{ type: 'label', name: 'hideRelations' },
{ type: 'label', name: 'titleTemplate' },

// relation names
{ type: 'relation', name: 'internalLink' },
Expand Down
27 changes: 24 additions & 3 deletions src/services/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const becca = require('../becca/becca');
const Branch = require('../becca/entities/branch');
const Note = require('../becca/entities/note');
const Attribute = require('../becca/entities/attribute');
const TaskContext = require("./task_context.js");
const dayjs = require("dayjs");

function getNewNotePosition(parentNoteId) {
const note = becca.notes[parentNoteId];
Expand Down Expand Up @@ -79,6 +79,28 @@ function copyChildAttributes(parentNote, childNote) {
}
}

function getNewNoteTitle(parentNote) {
let title = "new note";

const titleTemplate = parentNote.getLabelValue('titleTemplate');

if (titleTemplate !== null) {
try {
const now = dayjs(cls.getLocalNowDateTime() || new Date());

// "officially" injected values:
// - now
// - parentNote

title = eval('`' + titleTemplate + '`');
} catch (e) {
log.error(`Title template of note '${parentNote.noteId}' failed with: ${e.message}`);
}
}

return title;
}

/**
* Following object properties are mandatory:
* - {string} parentNoteId
Expand All @@ -104,8 +126,7 @@ function createNewNote(params) {
}

if (params.title === null || params.title === undefined) {
// empty title is allowed since it's possible to create such in the UI
throw new Error(`Note title must be set`);
params.title = getNewNoteTitle(parentNote);
}

if (params.content === null || params.content === undefined) {
Expand Down

0 comments on commit 593a275

Please sign in to comment.