added shareAlias label

This commit is contained in:
zadam 2021-12-22 10:57:02 +01:00
parent bcef8579ce
commit c0964a4f12
10 changed files with 49 additions and 11 deletions

View File

@ -210,7 +210,9 @@ const ATTR_HELP = {
"hoistedInbox": "default inbox location for new notes when hoisted to some ancestor of this note", "hoistedInbox": "default inbox location for new notes when hoisted to some ancestor of this note",
"sqlConsoleHome": "default location of SQL console notes", "sqlConsoleHome": "default location of SQL console notes",
"bookmarked": "note with this label will appear in bookmarks", "bookmarked": "note with this label will appear in bookmarks",
"bookmarkFolder": "note with this label will appear in bookmarks as folder (allowing access to its children)" "bookmarkFolder": "note with this label will appear in bookmarks as folder (allowing access to its children)",
"shareHiddenFromTree": "this note is hidden from left navigation tree, but still accessible with its URL",
"shareAlias": "define an alias using which the note will be available under https://your_trilium_host/share/[your_alias]",
}, },
"relation": { "relation": {
"runOnNoteCreation": "executes when note is created on backend", "runOnNoteCreation": "executes when note is created on backend",
@ -221,7 +223,8 @@ const ATTR_HELP = {
"runOnAttributeChange": "executes when attribute is changed under this note", "runOnAttributeChange": "executes when attribute is changed under this note",
"template": "attached note's attributes will be inherited even without parent-child relationship. See template for details.", "template": "attached note's attributes will be inherited even without parent-child relationship. See template for details.",
"renderNote": 'notes of type "render HTML note" will be rendered using a code note (HTML or script) and it is necessary to point using this relation to which note should be rendered', "renderNote": 'notes of type "render HTML note" will be rendered using a code note (HTML or script) and it is necessary to point using this relation to which note should be rendered',
"widget": "target of this relation will be executed and rendered as a widget in the sidebar" "widget": "target of this relation will be executed and rendered as a widget in the sidebar",
"shareCss": "CSS note which will be injected into the share page. CSS note must be in the shared sub-tree as well. Consider using 'shareHiddenFromTree' label to hide it.",
} }
}; };

View File

@ -1,5 +1,6 @@
import NoteContextAwareWidget from "./note_context_aware_widget.js"; import NoteContextAwareWidget from "./note_context_aware_widget.js";
import options from "../services/options.js"; import options from "../services/options.js";
import attributeService from "../services/attributes.js";
const TPL = ` const TPL = `
<div class="shared-info-widget alert alert-warning"> <div class="shared-info-widget alert alert-warning">
@ -31,15 +32,23 @@ export default class SharedInfoWidget extends NoteContextAwareWidget {
const syncServerHost = options.get("syncServerHost"); const syncServerHost = options.get("syncServerHost");
let link; let link;
const shareId = note.getOwnedLabelValue('shareAlias') || note.noteId;
if (syncServerHost) { if (syncServerHost) {
link = syncServerHost + "/share/" + note.noteId; link = syncServerHost + "/share/" + shareId;
this.$shareText.text("This note is shared publicly on"); this.$shareText.text("This note is shared publicly on");
} }
else { else {
link = location.protocol + '//' + location.host + location.pathname + "share/" + note.noteId; link = location.protocol + '//' + location.host + location.pathname + "share/" + shareId;
this.$shareText.text("This note is shared locally on"); this.$shareText.text("This note is shared locally on");
} }
this.$shareLink.attr("href", link).text(link); this.$shareLink.attr("href", link).text(link);
} }
entitiesReloadedEvent({loadResults}) {
if (loadResults.getAttributes().find(attr => attr.name.startsWith("share") && attributeService.isAffecting(attr, this.note))) {
this.refresh();
}
}
} }

View File

@ -53,6 +53,7 @@ const BUILTIN_ATTRIBUTES = [
{ type: 'label', name: 'top' }, { type: 'label', name: 'top' },
{ type: 'label', name: 'fullContentWidth' }, { type: 'label', name: 'fullContentWidth' },
{ type: 'label', name: 'shareHiddenFromTree' }, { type: 'label', name: 'shareHiddenFromTree' },
{ type: 'label', name: 'shareAlias' },
// relation names // relation names
{ type: 'relation', name: 'runOnNoteCreation', isDangerous: true }, { type: 'relation', name: 'runOnNoteCreation', isDangerous: true },

View File

@ -1,5 +1,6 @@
const {JSDOM} = require("jsdom"); const {JSDOM} = require("jsdom");
const NO_CONTENT = '<p>This note has no content.</p>'; const NO_CONTENT = '<p>This note has no content.</p>';
const shaca = require("./shaca/shaca");
function getChildrenList(note) { function getChildrenList(note) {
if (note.hasChildren()) { if (note.hasChildren()) {
@ -43,7 +44,15 @@ function getContent(note) {
if (href?.startsWith("#")) { if (href?.startsWith("#")) {
const notePathSegments = href.split("/"); const notePathSegments = href.split("/");
linkEl.setAttribute("href", notePathSegments[notePathSegments.length - 1]); const noteId = notePathSegments[notePathSegments.length - 1];
const linkedNote = shaca.getNote(noteId);
if (linkedNote) {
linkEl.setAttribute("href", linkedNote.shareId);
}
else {
linkEl.removeAttribute("href");
}
} }
} }

View File

@ -18,14 +18,14 @@ function getSubRoot(note) {
} }
function register(router) { function register(router) {
router.get('/share/:noteId', (req, res, next) => { router.get('/share/:shareId', (req, res, next) => {
const {noteId} = req.params; const {shareId} = req.params;
shacaLoader.ensureLoad(); shacaLoader.ensureLoad();
if (noteId in shaca.notes) { const note = shaca.aliasToNote[shareId] || shaca.notes[shareId];
const note = shaca.notes[noteId];
if (note) {
const content = contentRenderer.getContent(note); const content = contentRenderer.getContent(note);
const subRoot = getSubRoot(note); const subRoot = getSubRoot(note);

View File

@ -39,6 +39,10 @@ class Attribute extends AbstractEntity {
linkedChildNote.parents = linkedChildNote.parents.filter(parentNote => parentNote.noteId !== this.noteId); linkedChildNote.parents = linkedChildNote.parents.filter(parentNote => parentNote.noteId !== this.noteId);
} }
} }
if (this.type === 'label' && this.name === 'shareAlias' && this.value.trim()) {
this.shaca.aliasToNote[this.value.trim()] = this.note;
}
} }
get isAffectingSubtree() { get isAffectingSubtree() {

View File

@ -549,6 +549,12 @@ class Note extends AbstractEntity {
return notePaths.some(path => path.includes(ancestorNoteId)); return notePaths.some(path => path.includes(ancestorNoteId));
} }
get shareId() {
const sharedAlias = this.getOwnedLabelValue("shareAlias");
return sharedAlias || this.noteId;
}
} }
module.exports = Note; module.exports = Note;

View File

@ -14,6 +14,8 @@ class Shaca {
this.childParentToBranch = {}; this.childParentToBranch = {};
/** @type {Object.<String, Attribute>} */ /** @type {Object.<String, Attribute>} */
this.attributes = {}; this.attributes = {};
/** @type {Object.<String, String>} */
this.aliasToNote = {};
this.loaded = false; this.loaded = false;
} }
@ -22,6 +24,10 @@ class Shaca {
return this.notes[noteId]; return this.notes[noteId];
} }
hasNote(noteId) {
return noteId in this.notes;
}
getNotes(noteIds, ignoreMissing = false) { getNotes(noteIds, ignoreMissing = false) {
const filteredNotes = []; const filteredNotes = [];

View File

@ -48,7 +48,7 @@ function load() {
WHERE isDeleted = 0 WHERE isDeleted = 0
AND noteId IN (${noteIdStr}) AND noteId IN (${noteIdStr})
AND ( AND (
(type = 'label' AND name IN ('archived', 'shareHiddenFromTree')) (type = 'label' AND name IN ('archived', 'shareHiddenFromTree', 'shareAlias'))
OR (type = 'relation' AND name IN ('imageLink', 'template', 'shareCss')) OR (type = 'relation' AND name IN ('imageLink', 'template', 'shareCss'))
)`, []); )`, []);

View File

@ -2,7 +2,7 @@
<% if (activeNote.noteId === note.noteId) { %> <% if (activeNote.noteId === note.noteId) { %>
<strong><%= note.title %></strong> <strong><%= note.title %></strong>
<% } else { %> <% } else { %>
<a href="./<%= note.noteId %>"><%= note.title %></a> <a href="./<%= note.shareId %>"><%= note.title %></a>
<% } %> <% } %>
</p> </p>