fix(export/share): tree not expanding properly

This commit is contained in:
Elian Doran 2025-06-24 17:44:47 +03:00
parent 413137ac64
commit a2110ca631
No known key found for this signature in database
4 changed files with 19 additions and 16 deletions

View File

@ -1,6 +1,6 @@
import { join } from "path";
import NoteMeta, { NoteMetaFile } from "../../meta/note_meta";
import { ZipExportProvider } from "./abstract_provider";
import { ZipExportProvider } from "./abstract_provider.js";
import { RESOURCE_DIR } from "../../resource_dir";
import { getResourceDir, isDev } from "../../utils";
import fs from "fs";
@ -50,7 +50,7 @@ export default class ShareThemeExportProvider extends ZipExportProvider {
const basePath = "../".repeat(noteMeta.notePath.length - 1);
if (note) {
content = renderNoteForExport(note, branch, basePath);
content = renderNoteForExport(note, branch, basePath, noteMeta.notePath.slice(0, -1));
content = content.replace(/href="[^"]*\.\/([a-zA-Z0-9_\/]{12})[^"]*"/g, "href=\"#root/$1\"");
content = this.rewriteFn(content, noteMeta);
}

View File

@ -61,7 +61,7 @@ function getSharedSubTreeRoot(note: SNote | BNote | undefined): Subroot {
return getSharedSubTreeRoot(parentBranch.getParentNote());
}
export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath: string) {
export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath: string, ancestors: string[]) {
const subRoot: Subroot = {
branch: parentBranch,
note: parentBranch.getNote()
@ -69,7 +69,7 @@ export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath
return renderNoteContentInternal(note, {
subRoot,
rootNoteId: note.getParentNotes()[0].noteId,
rootNoteId: parentBranch.noteId,
cssToLoad: [
`${basePath}style.css`,
`${basePath}boxicons.css`
@ -77,13 +77,22 @@ export function renderNoteForExport(note: BNote, parentBranch: BBranch, basePath
jsToLoad: [
`${basePath}script.js`
],
logoUrl: `${basePath}icon-color.svg`
logoUrl: `${basePath}icon-color.svg`,
ancestors
});
}
export function renderNoteContent(note: SNote) {
const subRoot = getSharedSubTreeRoot(note);
const ancestors: string[] = [];
let notePointer = note;
while (notePointer.parents[0].noteId !== subRoot.note?.noteId) {
const pointerParent = notePointer.parents[0];
ancestors.push(pointerParent.noteId);
notePointer = pointerParent;
}
// Determine CSS to load.
const cssToLoad: string[] = [];
if (!isDev && !note.isLabelTruthy("shareOmitDefaultCss")) {
@ -110,7 +119,8 @@ export function renderNoteContent(note: SNote) {
rootNoteId: "_share",
cssToLoad,
jsToLoad,
logoUrl
logoUrl,
ancestors
});
}
@ -120,6 +130,7 @@ interface RenderArgs {
cssToLoad: string[];
jsToLoad: string[];
logoUrl: string;
ancestors: string[];
}
function renderNoteContentInternal(note: SNote | BNote, renderArgs: RenderArgs) {

View File

@ -137,6 +137,7 @@ function register(router: Router) {
return;
}
res.send(renderNoteContent(note));
}

View File

@ -97,16 +97,7 @@ content = content.replaceAll(headingRe, (...match) => {
</div>
<% if (hasTree) { %>
<nav id="menu">
<%
const ancestors = [];
let notePointer = note;
while (notePointer.parents[0].noteId !== rootNoteId) {
const pointerParent = notePointer.parents[0];
ancestors.push(pointerParent.noteId);
notePointer = pointerParent;
}
%>
<%- include("tree_item", {note: subRoot.note, activeNote: note, subRoot: subRoot, ancestors: ancestors}) %>
<%- include("tree_item", {note: subRoot.note, activeNote: note, subRoot: subRoot, ancestors}) %>
</nav>
<% } %>
</div>