Skip to content

Commit 1f21c65

Browse files
author
x1arch
committed
update share path to .../parent/noteid
1 parent 5d5fd20 commit 1f21c65

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

apps/server/src/share/content_renderer.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,14 @@ function addContentAccessQuery(note: SNote | BNote, secondEl?:boolean) {
4747
return ""
4848
}
4949

50-
function getSharedSubTreeRoot(note: SNote | BNote | undefined): Subroot {
50+
export function getSharedSubTreeRoot(note: SNote | BNote | undefined, parentId: string | undefined = undefined): Subroot {
5151
if (!note || note.noteId === shareRoot.SHARE_ROOT_NOTE_ID) {
5252
// share root itself is not shared
5353
return {};
5454
}
5555

56-
// every path leads to share root, but which one to choose?
57-
// for the sake of simplicity, URLs are not note paths
58-
const parentBranch = note.getParentBranches()[0];
56+
const parentBranches = note.getParentBranches()
57+
const parentBranch = (parentId ? parentBranches.find((pb: SBranch | BBranch) => pb.parentNoteId === parentId) : undefined) || parentBranches[0];
5958

6059
if (note instanceof BNote) {
6160
return {
@@ -71,7 +70,7 @@ function getSharedSubTreeRoot(note: SNote | BNote | undefined): Subroot {
7170
};
7271
}
7372

74-
return getSharedSubTreeRoot(parentBranch.getParentNote());
73+
return getSharedSubTreeRoot(parentBranch.getParentNote(), parentId);
7574
}
7675

7776
export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath: string, ancestors: string[]) {
@@ -98,7 +97,7 @@ export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath
9897
}
9998

10099
export function renderNoteContent(note: SNote) {
101-
const subRoot = getSharedSubTreeRoot(note);
100+
const subRoot = getSharedSubTreeRoot(note, note.parentId);
102101

103102
const ancestors: string[] = [];
104103
let notePointer = note;
@@ -114,23 +113,23 @@ export function renderNoteContent(note: SNote) {
114113
// Determine CSS to load.
115114
const cssToLoad: string[] = [];
116115
if (!note.isLabelTruthy("shareOmitDefaultCss")) {
117-
cssToLoad.push(`assets/styles.css`);
118-
cssToLoad.push(`assets/scripts.css`);
116+
cssToLoad.push(`../assets/styles.css`);
117+
cssToLoad.push(`../assets/scripts.css`);
119118
}
120119
for (const cssRelation of note.getRelations("shareCss")) {
121-
cssToLoad.push(`api/notes/${cssRelation.value}/download${addContentAccessQuery(note)}`);
120+
cssToLoad.push(`../api/notes/${cssRelation.value}/download${addContentAccessQuery(note)}`);
122121
}
123122

124123
// Determine JS to load.
125124
const jsToLoad: string[] = [
126-
"assets/scripts.js"
125+
"../assets/scripts.js"
127126
];
128127
for (const jsRelation of note.getRelations("shareJs")) {
129-
jsToLoad.push(`api/notes/${jsRelation.value}/download${addContentAccessQuery(note)}`);
128+
jsToLoad.push(`../api/notes/${jsRelation.value}/download${addContentAccessQuery(note)}`);
130129
}
131130

132131
const customLogoId = note.getRelation("shareLogo")?.value;
133-
const logoUrl = customLogoId ? `api/images/${customLogoId}/image.png${addContentAccessQuery(note)}` : `../${assetUrlFragment}/images/icon-color.svg`;
132+
const logoUrl = customLogoId ? `../api/images/${customLogoId}/image.png${addContentAccessQuery(note)}` : `../../${assetUrlFragment}/images/icon-color.svg`;
134133

135134
return renderNoteContentInternal(note, {
136135
subRoot,
@@ -140,7 +139,7 @@ export function renderNoteContent(note: SNote) {
140139
logoUrl,
141140
ancestors,
142141
isStatic: false,
143-
faviconUrl: note.hasRelation("shareFavicon") ? `api/notes/${note.getRelationValue("shareFavicon")}/download${addContentAccessQuery(note)}` : `../favicon.ico`
142+
faviconUrl: note.hasRelation("shareFavicon") ? `../api/notes/${note.getRelationValue("shareFavicon")}/download${addContentAccessQuery(note)}` : `../../favicon.ico`
144143
});
145144
}
146145

@@ -365,7 +364,7 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string, getNote: GetNot
365364
const attachment = getAttachment(attachmentId);
366365

367366
if (attachment) {
368-
linkEl.setAttribute("href", `api/attachments/${attachmentId}/download${addContentAccessQuery(note)}`);
367+
linkEl.setAttribute("href", `../api/attachments/${attachmentId}/download${addContentAccessQuery(note)}`);
369368
linkEl.classList.add(`attachment-link`);
370369
linkEl.classList.add(`role-${attachment.role}`);
371370
linkEl.childNodes.length = 0;
@@ -381,7 +380,7 @@ function handleAttachmentLink(linkEl: HTMLElement, href: string, getNote: GetNot
381380
const linkedNote = getNote(noteId);
382381
if (linkedNote) {
383382
const isExternalLink = linkedNote.hasLabel("shareExternalLink");
384-
const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `./${linkedNote.shareId}`;
383+
const href = isExternalLink ? linkedNote.getLabelValue("shareExternalLink") : `../${linkedNote.shareId}`;
385384
if (href) {
386385
linkEl.setAttribute("href", href);
387386
}
@@ -438,7 +437,7 @@ function renderMermaid(result: Result, note: SNote | BNote) {
438437
}
439438

440439
result.content = `
441-
<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}${addContentAccessQuery(note, true)}">
440+
<img src="../api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}${addContentAccessQuery(note, true)}">
442441
<hr>
443442
<details>
444443
<summary>Chart source</summary>
@@ -447,14 +446,14 @@ function renderMermaid(result: Result, note: SNote | BNote) {
447446
}
448447

449448
function renderImage(result: Result, note: SNote | BNote) {
450-
result.content = `<img src="api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}${addContentAccessQuery(note, true)}">`;
449+
result.content = `<img src="../api/images/${note.noteId}/${note.encodedTitle}?${note.utcDateModified}${addContentAccessQuery(note, true)}">`;
451450
}
452451

453452
function renderFile(note: SNote | BNote, result: Result) {
454453
if (note.mime === "application/pdf") {
455-
result.content = `<iframe class="pdf-view" src="api/notes/${note.noteId}/view${addContentAccessQuery(note)}"></iframe>`;
454+
result.content = `<iframe class="pdf-view" src="../api/notes/${note.noteId}/view${addContentAccessQuery(note)}"></iframe>`;
456455
} else {
457-
result.content = `<button type="button" onclick="location.href='api/notes/${note.noteId}/download${addContentAccessQuery(note)}'">Download file</button>`;
456+
result.content = `<button type="button" onclick="location.href='../api/notes/${note.noteId}/download${addContentAccessQuery(note)}'">Download file</button>`;
458457
}
459458
}
460459

apps/server/src/share/routes.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import searchService from "../services/search/services/search.js";
88
import SearchContext from "../services/search/search_context.js";
99
import type SNote from "./shaca/entities/snote.js";
1010
import type SAttachment from "./shaca/entities/sattachment.js";
11-
import { getDefaultTemplatePath, renderNoteContent } from "./content_renderer.js";
11+
import { getDefaultTemplatePath, getSharedSubTreeRoot, renderNoteContent } from "./content_renderer.js";
1212
import utils from "../services/utils.js";
1313

1414
function addNoIndexHeader(note: SNote, res: Response) {
@@ -180,19 +180,31 @@ function register(router: Router) {
180180
renderNote(shaca.shareRootNote, req, res);
181181
});
182182

183-
router.get("/share/:shareId", (req, res) => {
183+
router.get("/share/:parentShareId/:shareId", (req, res) => {
184184
shacaLoader.ensureLoad();
185185

186-
const { shareId } = req.params;
186+
const { parentShareId, shareId } = req.params;
187187

188188
const note = shaca.aliasToNote[shareId] || shaca.notes[shareId];
189189
if (note){
190+
note.parentId = parentShareId
190191
note.initContentAccessor()
191192
}
192193

193194
renderNote(note, req, res);
194195
});
195196

197+
router.get("/share/:shareId", (req, res) => {
198+
shacaLoader.ensureLoad();
199+
200+
const { shareId } = req.params;
201+
202+
const note = shaca.aliasToNote[shareId] || shaca.notes[shareId];
203+
const parent = getSharedSubTreeRoot(note)
204+
205+
res.redirect(`${parent?.note?.noteId}/${shareId}`)
206+
});
207+
196208
router.get("/share/api/notes/:noteId", (req, res) => {
197209
shacaLoader.ensureLoad();
198210
let note: SNote | boolean;

apps/server/src/share/shaca/entities/snote.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const isCredentials = (attr: SAttribute) => attr.type === "label" && attr.name =
2020

2121
class SNote extends AbstractShacaEntity {
2222
noteId: string;
23+
parentId?: string | undefined;
2324
title: string;
2425
type: string;
2526
mime: string;
@@ -40,6 +41,7 @@ class SNote extends AbstractShacaEntity {
4041
super();
4142

4243
this.noteId = noteId;
44+
this.parentId = undefined;
4345
this.title = isProtected ? "[protected]" : title;
4446
this.type = type;
4547
this.mime = mime;
@@ -70,6 +72,10 @@ class SNote extends AbstractShacaEntity {
7072
}
7173
}
7274

75+
getParentId() {
76+
return this.parentId;
77+
}
78+
7379
getParentBranches() {
7480
return this.parentBranches;
7581
}

packages/share-theme/src/templates/page.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ content = content.replaceAll(headingRe, (...match) => {
157157
<%
158158
content = content
159159
.replace(/<img /g, `<img alt="${t("share_theme.image_alt")}" loading="lazy" `)
160-
.replace(/src="(api\/[^"]+)"/g, (m, url) => `src="${url}${addContentAccessQuery(url.includes('?'))}"`);
160+
.replace(/src="(api\/[^"]+)"/g, (m, url) => `src="../${url}${addContentAccessQuery(url.includes('?'))}"`);
161161
%>
162162
<%- content %>
163163
<% } %>

0 commit comments

Comments
 (0)