add possibility to define a share index, closes #3265

This commit is contained in:
zadam 2022-11-01 22:49:37 +01:00
parent eb68ab6776
commit 2467464433
8 changed files with 147 additions and 87 deletions

View File

@ -223,6 +223,7 @@ const ATTR_HELP = {
"shareRaw": "note will be served in its raw format, without HTML wrapper", "shareRaw": "note will be served in its raw format, without HTML wrapper",
"shareDisallowRobotIndexing": `will forbid robot indexing of this note via <code>X-Robots-Tag: noindex</code> header`, "shareDisallowRobotIndexing": `will forbid robot indexing of this note via <code>X-Robots-Tag: noindex</code> header`,
"shareCredentials": "require credentials to access this shared note. Value is expected to be in format 'username:password'. Don't forget to make this inheritable to apply to child-notes/images.", "shareCredentials": "require credentials to access this shared note. Value is expected to be in format 'username:password'. Don't forget to make this inheritable to apply to child-notes/images.",
"shareIndex": "note with this this label will list all roots of shared notes",
"displayRelations": "comma delimited names of relations which should be displayed. All other ones will be hidden.", "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.", "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 "titleTemplate": `default title of notes created as children of this note. The value is evaluated as JavaScript string

View File

@ -54,6 +54,7 @@ module.exports = [
{ type: 'label', name: 'shareRaw' }, { type: 'label', name: 'shareRaw' },
{ type: 'label', name: 'shareDisallowRobotIndexing' }, { type: 'label', name: 'shareDisallowRobotIndexing' },
{ type: 'label', name: 'shareCredentials' }, { type: 'label', name: 'shareCredentials' },
{ type: 'label', name: 'shareIndex' },
{ type: 'label', name: 'displayRelations' }, { type: 'label', name: 'displayRelations' },
{ type: 'label', name: 'hideRelations' }, { type: 'label', name: 'hideRelations' },
{ type: 'label', name: 'titleTemplate', isDangerous: true }, { type: 'label', name: 'titleTemplate', isDangerous: true },

View File

@ -1,6 +1,7 @@
const {JSDOM} = require("jsdom"); const {JSDOM} = require("jsdom");
const shaca = require("./shaca/shaca"); const shaca = require("./shaca/shaca");
const assetPath = require("../services/asset_path"); const assetPath = require("../services/asset_path");
const shareRoot = require('./share_root');
function getContent(note) { function getContent(note) {
if (note.isProtected) { if (note.isProtected) {
@ -11,17 +12,52 @@ function getContent(note) {
}; };
} }
let content = note.getContent(); const result = {
let header = ''; content: note.getContent(),
let isEmpty = false; header: '',
isEmpty: false
};
if (note.type === 'text') { if (note.type === 'text') {
const document = new JSDOM(content || "").window.document; renderText(result, note);
} else if (note.type === 'code') {
renderCode(result);
} else if (note.type === 'mermaid') {
renderMermaid(result);
} else if (note.type === 'image') {
renderImage(result, note);
} else if (note.type === 'file') {
renderFile(note, result);
} else if (note.type === 'book') {
result.isEmpty = true;
} else if (note.type === 'canvas') {
renderCanvas(result, note);
} else {
result.content = '<p>This note type cannot be displayed.</p>';
}
isEmpty = document.body.textContent.trim().length === 0 return result;
}
function renderIndex(result) {
result.content += '<ul id="index">';
const rootNote = shaca.getNote(shareRoot.SHARE_ROOT_NOTE_ID);
for (const childNote of rootNote.getChildNotes()) {
result.content += `<li><a class="${childNote.type}" href="./${childNote.shareId}">${childNote.escapedTitle}</a></li>`;
}
result.content += '</ul>';
}
function renderText(result, note) {
const document = new JSDOM(result.content || "").window.document;
result.isEmpty = document.body.textContent.trim().length === 0
&& document.querySelectorAll("img").length === 0; && document.querySelectorAll("img").length === 0;
if (!isEmpty) { if (!result.isEmpty) {
for (const linkEl of document.querySelectorAll("a")) { for (const linkEl of document.querySelectorAll("a")) {
const href = linkEl.getAttribute("href"); const href = linkEl.getAttribute("href");
@ -34,17 +70,16 @@ function getContent(note) {
if (linkedNote) { if (linkedNote) {
linkEl.setAttribute("href", linkedNote.shareId); linkEl.setAttribute("href", linkedNote.shareId);
linkEl.classList.add("type-" + linkedNote.type); linkEl.classList.add("type-" + linkedNote.type);
} } else {
else {
linkEl.removeAttribute("href"); linkEl.removeAttribute("href");
} }
} }
} }
content = document.body.innerHTML; result.content = document.body.innerHTML;
if (content.includes(`<span class="math-tex">`)) { if (result.content.includes(`<span class="math-tex">`)) {
header += ` result.header += `
<script src="../../${assetPath}/libraries/katex/katex.min.js"></script> <script src="../../${assetPath}/libraries/katex/katex.min.js"></script>
<link rel="stylesheet" href="../../${assetPath}/libraries/katex/katex.min.css"> <link rel="stylesheet" href="../../${assetPath}/libraries/katex/katex.min.css">
<script src="../../${assetPath}/libraries/katex/auto-render.min.js"></script> <script src="../../${assetPath}/libraries/katex/auto-render.min.js"></script>
@ -55,53 +90,57 @@ document.addEventListener("DOMContentLoaded", function() {
}); });
</script>`; </script>`;
} }
if (note.hasLabel("shareIndex")) {
renderIndex(result);
} }
} }
else if (note.type === 'code') {
if (!content?.trim()) {
isEmpty = true;
} }
else {
function renderCode(result) {
if (!result.content?.trim()) {
result.isEmpty = true;
} else {
const document = new JSDOM().window.document; const document = new JSDOM().window.document;
const preEl = document.createElement('pre'); const preEl = document.createElement('pre');
preEl.appendChild(document.createTextNode(content)); preEl.appendChild(document.createTextNode(result.content));
content = preEl.outerHTML; result.content = preEl.outerHTML;
} }
} }
else if (note.type === 'mermaid') {
content = ` function renderMermaid(result) {
<div class="mermaid">${content}</div> result.content = `
<div class="mermaid">${result.content}</div>
<hr> <hr>
<details> <details>
<summary>Chart source</summary> <summary>Chart source</summary>
<pre>${content}</pre> <pre>${result.content}</pre>
</details>` </details>`
header += `<script src="../../${assetPath}/libraries/mermaid.min.js"></script>`; result.header += `<script src="../../${assetPath}/libraries/mermaid.min.js"></script>`;
} }
else if (note.type === 'image') {
content = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`; function renderImage(result, note) {
result.content = `<img src="api/images/${note.noteId}/${note.title}?${note.utcDateModified}">`;
} }
else if (note.type === 'file') {
function renderFile(note, result) {
if (note.mime === 'application/pdf') { if (note.mime === 'application/pdf') {
content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>` result.content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view"></iframe>`
} } else {
else { result.content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download'">Download file</button>`;
} }
} }
else if (note.type === 'book') {
isEmpty = true; function renderCanvas(result, note) {
} result.header += `<script>
else if (note.type === 'canvas') {
header += `<script>
window.EXCALIDRAW_ASSET_PATH = window.location.origin + "/node_modules/@excalidraw/excalidraw/dist/"; window.EXCALIDRAW_ASSET_PATH = window.location.origin + "/node_modules/@excalidraw/excalidraw/dist/";
</script>`; </script>`;
header += `<script src="../../${assetPath}/node_modules/react/umd/react.production.min.js"></script>`; result.header += `<script src="../../${assetPath}/node_modules/react/umd/react.production.min.js"></script>`;
header += `<script src="../../${assetPath}/node_modules/react-dom/umd/react-dom.production.min.js"></script>`; result.header += `<script src="../../${assetPath}/node_modules/react-dom/umd/react-dom.production.min.js"></script>`;
header += `<script src="../../${assetPath}/node_modules/@excalidraw/excalidraw/dist/excalidraw.production.min.js"></script>`; result.header += `<script src="../../${assetPath}/node_modules/@excalidraw/excalidraw/dist/excalidraw.production.min.js"></script>`;
header += `<style> result.header += `<style>
.excalidraw-wrapper { .excalidraw-wrapper {
height: 100%; height: 100%;
@ -115,27 +154,17 @@ document.addEventListener("DOMContentLoaded", function() {
} }
</style>`; </style>`;
content = `<div> result.content = `<div>
<script> <script>
const {elements, appState, files} = JSON.parse(${JSON.stringify(content)}); const {elements, appState, files} = JSON.parse(${JSON.stringify(result.content)});
window.triliumExcalidraw = {elements, appState, files} window.triliumExcalidraw = {elements, appState, files}
</script> </script>
<div id="excalidraw-app"></div> <div id="excalidraw-app"></div>
<hr> <hr>
<a href="api/images/${note.noteId}/${note.title}?utc=${note.utcDateModified}">Get Image Link</a> <a href="api/images/${note.noteId}/${note.escapedTitle}?utc=${note.utcDateModified}">Get Image Link</a>
<script src="./canvas_share.js"></script> <script src="./canvas_share.js"></script>
</div>`; </div>`;
} }
else {
content = '<p>This note type cannot be displayed.</p>';
}
return {
header,
content,
isEmpty
};
}
module.exports = { module.exports = {
getContent getContent

View File

@ -88,7 +88,7 @@ function register(router) {
addNoIndexHeader(note, res); addNoIndexHeader(note, res);
if (note.hasLabel('shareRaw') || ['image', 'file'].includes(note.type)) { if (note.hasLabel('shareRaw')) {
res.setHeader('Content-Type', note.mime) res.setHeader('Content-Type', note.mime)
.send(note.getContent()); .send(note.getContent());

View File

@ -49,39 +49,40 @@ class Attribute extends AbstractEntity {
} }
} }
/** @returns {boolean} */
get isAffectingSubtree() { get isAffectingSubtree() {
return this.isInheritable return this.isInheritable
|| (this.type === 'relation' && this.name === 'template'); || (this.type === 'relation' && this.name === 'template');
} }
/** @returns {string} */
get targetNoteId() { // alias get targetNoteId() { // alias
return this.type === 'relation' ? this.value : undefined; return this.type === 'relation' ? this.value : undefined;
} }
/** @returns {boolean} */
isAutoLink() { isAutoLink() {
return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name); return this.type === 'relation' && ['internalLink', 'imageLink', 'relationMapLink', 'includeNoteLink'].includes(this.name);
} }
/** @returns {Note|null} */
get note() { get note() {
return this.shaca.notes[this.noteId]; return this.shaca.notes[this.noteId];
} }
/** @returns {Note|null} */
get targetNote() { get targetNote() {
if (this.type === 'relation') { if (this.type === 'relation') {
return this.shaca.notes[this.value]; return this.shaca.notes[this.value];
} }
} }
/** /** @returns {Note|null} */
* @returns {Note|null}
*/
getNote() { getNote() {
return this.shaca.getNote(this.noteId); return this.shaca.getNote(this.noteId);
} }
/** /** @returns {Note|null} */
* @returns {Note|null}
*/
getTargetNote() { getTargetNote() {
if (this.type !== 'relation') { if (this.type !== 'relation') {
throw new Error(`Attribute ${this.attributeId} is not relation`); throw new Error(`Attribute ${this.attributeId} is not relation`);

View File

@ -43,6 +43,7 @@ class Branch extends AbstractEntity {
return this.shaca.notes[this.noteId]; return this.shaca.notes[this.noteId];
} }
/** @return {Note} */
getNote() { getNote() {
return this.childNote; return this.childNote;
} }

View File

@ -3,6 +3,7 @@
const sql = require('../../sql'); const sql = require('../../sql');
const utils = require('../../../services/utils'); const utils = require('../../../services/utils');
const AbstractEntity = require('./abstract_entity'); const AbstractEntity = require('./abstract_entity');
const escape = require('escape-html');
const LABEL = 'label'; const LABEL = 'label';
const RELATION = 'relation'; const RELATION = 'relation';
@ -47,22 +48,32 @@ class Note extends AbstractEntity {
this.shaca.notes[this.noteId] = this; this.shaca.notes[this.noteId] = this;
} }
/** @returns {Branch[]} */
getParentBranches() { getParentBranches() {
return this.parentBranches; return this.parentBranches;
} }
/** @returns {Branch[]} */
getBranches() { getBranches() {
return this.parentBranches; return this.parentBranches;
} }
/** @returns {Branch[]} */
getChildBranches() {
return this.children.map(childNote => this.shaca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
}
/** @returns {Note[]} */
getParentNotes() { getParentNotes() {
return this.parents; return this.parents;
} }
/** @returns {Note[]} */
getChildNotes() { getChildNotes() {
return this.children; return this.children;
} }
/** @returns {Note[]} */
getVisibleChildNotes() { getVisibleChildNotes() {
return this.getChildBranches() return this.getChildBranches()
.filter(branch => !branch.isHidden) .filter(branch => !branch.isHidden)
@ -70,18 +81,16 @@ class Note extends AbstractEntity {
.filter(childNote => !childNote.hasLabel('shareHiddenFromTree')); .filter(childNote => !childNote.hasLabel('shareHiddenFromTree'));
} }
/** @returns {boolean} */
hasChildren() { hasChildren() {
return this.children && this.children.length > 0; return this.children && this.children.length > 0;
} }
/** @returns {boolean} */
hasVisibleChildren() { hasVisibleChildren() {
return this.getVisibleChildNotes().length > 0; return this.getVisibleChildNotes().length > 0;
} }
getChildBranches() {
return this.children.map(childNote => this.shaca.getBranchFromChildAndParent(childNote.noteId, this.noteId));
}
getContent(silentNotFoundError = false) { getContent(silentNotFoundError = false) {
const row = sql.getRow(`SELECT content FROM note_contents WHERE noteId = ?`, [this.noteId]); const row = sql.getRow(`SELECT content FROM note_contents WHERE noteId = ?`, [this.noteId]);
@ -133,6 +142,7 @@ class Note extends AbstractEntity {
} }
} }
/** @returns {Attribute[]} */
getCredentials() { getCredentials() {
this.__getAttributes([]); this.__getAttributes([]);
@ -203,10 +213,12 @@ class Note extends AbstractEntity {
return this.inheritableAttributeCache; return this.inheritableAttributeCache;
} }
/** @returns {boolean} */
hasAttribute(type, name) { hasAttribute(type, name) {
return !!this.getAttributes().find(attr => attr.type === type && attr.name === name); return !!this.getAttributes().find(attr => attr.type === type && attr.name === name);
} }
/** @returns {Note|null} */
getRelationTarget(name) { getRelationTarget(name) {
const relation = this.getAttributes().find(attr => attr.type === 'relation' && attr.name === name); const relation = this.getAttributes().find(attr => attr.type === 'relation' && attr.name === name);
@ -411,22 +423,27 @@ class Note extends AbstractEntity {
return attrs.length > 0 ? attrs[0] : null; return attrs.length > 0 ? attrs[0] : null;
} }
/** @returns {boolean} */
get isArchived() { get isArchived() {
return this.hasAttribute('label', 'archived'); return this.hasAttribute('label', 'archived');
} }
/** @returns {boolean} */
hasInheritableOwnedArchivedLabel() { hasInheritableOwnedArchivedLabel() {
return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable); return !!this.ownedAttributes.find(attr => attr.type === 'label' && attr.name === 'archived' && attr.isInheritable);
} }
/** @returns {boolean} */
isTemplate() { isTemplate() {
return !!this.targetRelations.find(rel => rel.name === 'template'); return !!this.targetRelations.find(rel => rel.name === 'template');
} }
/** @returns {Attribute[]} */
getTargetRelations() { getTargetRelations() {
return this.targetRelations; return this.targetRelations;
} }
/** @returns {string} */
get shareId() { get shareId() {
if (this.hasOwnedLabel('shareRoot')) { if (this.hasOwnedLabel('shareRoot')) {
return ""; return "";
@ -437,6 +454,10 @@ class Note extends AbstractEntity {
return sharedAlias || this.noteId; return sharedAlias || this.noteId;
} }
get escapedTitle() {
return escape(this.title);
}
getPojoWithAttributes() { getPojoWithAttributes() {
return { return {
noteId: this.noteId, noteId: this.noteId,

View File

@ -23,14 +23,17 @@ class Shaca {
this.loaded = false; this.loaded = false;
} }
/** @returns {Note|null} */
getNote(noteId) { getNote(noteId) {
return this.notes[noteId]; return this.notes[noteId];
} }
/** @returns {boolean} */
hasNote(noteId) { hasNote(noteId) {
return noteId in this.notes; return noteId in this.notes;
} }
/** @returns {Note[]} */
getNotes(noteIds, ignoreMissing = false) { getNotes(noteIds, ignoreMissing = false) {
const filteredNotes = []; const filteredNotes = [];
@ -51,18 +54,21 @@ class Shaca {
return filteredNotes; return filteredNotes;
} }
/** @returns {Branch|null} */
getBranch(branchId) { getBranch(branchId) {
return this.branches[branchId]; return this.branches[branchId];
} }
getAttribute(attributeId) { /** @returns {Branch|null} */
return this.attributes[attributeId];
}
getBranchFromChildAndParent(childNoteId, parentNoteId) { getBranchFromChildAndParent(childNoteId, parentNoteId) {
return this.childParentToBranch[`${childNoteId}-${parentNoteId}`]; return this.childParentToBranch[`${childNoteId}-${parentNoteId}`];
} }
/** @returns {Attribute|null} */
getAttribute(attributeId) {
return this.attributes[attributeId];
}
getEntity(entityName, entityId) { getEntity(entityName, entityId) {
if (!entityName || !entityId) { if (!entityName || !entityId) {
return null; return null;