Compare commits

...

2 Commits

Author SHA1 Message Date
Jacky Zhao
b00198b888 fix: load mermaid as normal now that inline is safely below bundle size
Some checks are pending
Build and Test / build-and-test (macos-latest) (push) Waiting to run
Build and Test / build-and-test (windows-latest) (push) Waiting to run
Build and Test / build-and-test (ubuntu-latest) (push) Has been skipped
Build and Test / publish-tag (push) Has been skipped
2025-03-10 16:42:08 -07:00
Jacky Zhao
9e3e711646 fix: mermaid script load order 2025-03-10 16:20:08 -07:00
6 changed files with 48 additions and 40 deletions

View File

@ -31,13 +31,13 @@ If you prefer instructions in a video format you can try following Nicole van de
## 🔧 Features ## 🔧 Features
- [[Obsidian compatibility]], [[full-text search]], [[graph view]], note transclusion, [[wikilinks]], [[backlinks]], [[features/Latex|Latex]], [[syntax highlighting]], [[popover previews]], [[Docker Support]], [[i18n|internationalization]], [[comments]] and [many more](./features) right out of the box - [[Obsidian compatibility]], [[full-text search]], [[graph view]], note transclusion, [[wikilinks]], [[backlinks]], [[features/Latex|Latex]], [[syntax highlighting]], [[popover previews]], [[Docker Support]], [[i18n|internationalization]], [[comments]] and [many more](./features/) right out of the box
- Hot-reload for both configuration and content - Hot-reload for both configuration and content
- Simple JSX layouts and [[creating components|page components]] - Simple JSX layouts and [[creating components|page components]]
- [[SPA Routing|Ridiculously fast page loads]] and tiny bundle sizes - [[SPA Routing|Ridiculously fast page loads]] and tiny bundle sizes
- Fully-customizable parsing, filtering, and page generation through [[making plugins|plugins]] - Fully-customizable parsing, filtering, and page generation through [[making plugins|plugins]]
For a comprehensive list of features, visit the [features page](/features). You can read more about the _why_ behind these features on the [[philosophy]] page and a technical overview on the [[architecture]] page. For a comprehensive list of features, visit the [features page](./features/). You can read more about the _why_ behind these features on the [[philosophy]] page and a technical overview on the [[architecture]] page.
### 🚧 Troubleshooting + Updating ### 🚧 Troubleshooting + Updating

View File

@ -9,9 +9,6 @@ import { visit } from "unist-util-visit"
import { Root, Element, ElementContent } from "hast" import { Root, Element, ElementContent } from "hast"
import { GlobalConfiguration } from "../cfg" import { GlobalConfiguration } from "../cfg"
import { i18n } from "../i18n" import { i18n } from "../i18n"
// @ts-ignore
import mermaidScript from "./scripts/mermaid.inline"
import mermaidStyle from "./styles/mermaid.inline.scss"
import { QuartzPluginData } from "../plugins/vfile" import { QuartzPluginData } from "../plugins/vfile"
interface RenderComponents { interface RenderComponents {
@ -58,17 +55,6 @@ export function pageResources(
additionalHead: staticResources.additionalHead, additionalHead: staticResources.additionalHead,
} }
if (fileData.hasMermaidDiagram) {
resources.js.push({
script: mermaidScript,
loadTime: "afterDOMReady",
moduleType: "module",
contentType: "inline",
})
resources.css.push({ content: mermaidStyle, inline: true })
}
// NOTE: we have to put this last to make sure spa.inline.ts is the last item.
resources.js.push({ resources.js.push({
src: joinSegments(baseDir, "postscript.js"), src: joinSegments(baseDir, "postscript.js"),
loadTime: "afterDOMReady", loadTime: "afterDOMReady",

View File

@ -28,17 +28,15 @@ function setupCallout() {
) as HTMLCollectionOf<HTMLElement> ) as HTMLCollectionOf<HTMLElement>
for (const div of collapsible) { for (const div of collapsible) {
const title = div.firstElementChild const title = div.firstElementChild
if (!title) continue
if (title) { title.addEventListener("click", toggleCallout)
title.addEventListener("click", toggleCallout) window.addCleanup(() => title.removeEventListener("click", toggleCallout))
window.addCleanup(() => title.removeEventListener("click", toggleCallout))
const collapsed = div.classList.contains("is-collapsed") const collapsed = div.classList.contains("is-collapsed")
const height = collapsed ? title.scrollHeight : div.scrollHeight const height = collapsed ? title.scrollHeight : div.scrollHeight
div.style.maxHeight = height + "px" div.style.maxHeight = height + "px"
}
} }
} }
document.addEventListener("nav", setupCallout) document.addEventListener("nav", setupCallout)
window.addEventListener("resize", setupCallout)

View File

@ -1,4 +1,4 @@
import { removeAllChildren } from "./util" import { registerEscapeHandler, removeAllChildren } from "./util"
interface Position { interface Position {
x: number x: number
@ -158,7 +158,7 @@ document.addEventListener("nav", async () => {
) )
mermaidImport ||= await import( mermaidImport ||= await import(
//@ts-ignore // @ts-ignore
"https://cdnjs.cloudflare.com/ajax/libs/mermaid/11.4.0/mermaid.esm.min.mjs" "https://cdnjs.cloudflare.com/ajax/libs/mermaid/11.4.0/mermaid.esm.min.mjs"
) )
const mermaid = mermaidImport.default const mermaid = mermaidImport.default
@ -237,12 +237,12 @@ document.addEventListener("nav", async () => {
closeBtn.addEventListener("click", hideMermaid) closeBtn.addEventListener("click", hideMermaid)
expandBtn.addEventListener("click", showMermaid) expandBtn.addEventListener("click", showMermaid)
registerEscapeHandler(popupContainer, hideMermaid)
document.addEventListener("keydown", handleEscape) document.addEventListener("keydown", handleEscape)
window.addCleanup(() => { window.addCleanup(() => {
closeBtn.removeEventListener("click", hideMermaid) closeBtn.removeEventListener("click", hideMermaid)
expandBtn.removeEventListener("click", showMermaid) expandBtn.removeEventListener("click", showMermaid)
document.removeEventListener("keydown", handleEscape)
}) })
} }
}) })

View File

@ -56,8 +56,10 @@ function startLoading() {
}, 100) }, 100)
} }
let isNavigating = false
let p: DOMParser let p: DOMParser
async function navigate(url: URL, isBack: boolean = false) { async function _navigate(url: URL, isBack: boolean = false) {
isNavigating = true
startLoading() startLoading()
p = p || new DOMParser() p = p || new DOMParser()
const contents = await fetchCanonical(url) const contents = await fetchCanonical(url)
@ -128,6 +130,19 @@ async function navigate(url: URL, isBack: boolean = false) {
delete announcer.dataset.persist delete announcer.dataset.persist
} }
async function navigate(url: URL, isBack: boolean = false) {
if (isNavigating) return
isNavigating = true
try {
await _navigate(url, isBack)
} catch (e) {
console.error(e)
window.location.assign(url)
} finally {
isNavigating = false
}
}
window.spaNavigate = navigate window.spaNavigate = navigate
function createRouter() { function createRouter() {
@ -145,21 +160,13 @@ function createRouter() {
return return
} }
try { navigate(url, false)
navigate(url, false)
} catch (e) {
window.location.assign(url)
}
}) })
window.addEventListener("popstate", (event) => { window.addEventListener("popstate", (event) => {
const { url } = getOpts(event) ?? {} const { url } = getOpts(event) ?? {}
if (window.location.hash && window.location.pathname === url?.pathname) return if (window.location.hash && window.location.pathname === url?.pathname) return
try { navigate(new URL(window.location.toString()), true)
navigate(new URL(window.location.toString()), true)
} catch (e) {
window.location.reload()
}
return return
}) })
} }

View File

@ -16,9 +16,12 @@ import path from "path"
import { splitAnchor } from "../../util/path" import { splitAnchor } from "../../util/path"
import { JSResource, CSSResource } from "../../util/resources" import { JSResource, CSSResource } from "../../util/resources"
// @ts-ignore // @ts-ignore
import calloutScript from "../../components/scripts/callout.inline.ts" import calloutScript from "../../components/scripts/callout.inline"
// @ts-ignore // @ts-ignore
import checkboxScript from "../../components/scripts/checkbox.inline.ts" import checkboxScript from "../../components/scripts/checkbox.inline"
// @ts-ignore
import mermaidScript from "../../components/scripts/mermaid.inline"
import mermaidStyle from "../../components/styles/mermaid.inline.scss"
import { FilePath, pathToRoot, slugTag, slugifyFilePath } from "../../util/path" import { FilePath, pathToRoot, slugTag, slugifyFilePath } from "../../util/path"
import { toHast } from "mdast-util-to-hast" import { toHast } from "mdast-util-to-hast"
import { toHtml } from "hast-util-to-html" import { toHtml } from "hast-util-to-html"
@ -806,6 +809,20 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>>
}) })
} }
if (opts.mermaid) {
js.push({
script: mermaidScript,
loadTime: "afterDOMReady",
contentType: "inline",
moduleType: "module",
})
css.push({
content: mermaidStyle,
inline: true,
})
}
return { js, css } return { js, css }
}, },
} }