refactor(markdown): extract post-render effects into composable
Four components duplicated the watch -> nextTick -> listenToClick -> runTikz shape. Collapse to useMarkdownPostRender, which also accepts optional flags for Mermaid, Shikiji and image hydration so StackedNote no longer needs its own inline conditional chain.
This commit is contained in:
54
src/hooks/useMarkdownPostRender.hook.ts
Normal file
54
src/hooks/useMarkdownPostRender.hook.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { nextTick, type Ref, toValue, watch } from "vue"
|
||||
|
||||
import { useImages } from "@/hooks/useImages.hook"
|
||||
import {
|
||||
runMermaid,
|
||||
runTikz,
|
||||
useShikiji
|
||||
} from "@/hooks/useMarkdown.hook"
|
||||
|
||||
interface MarkdownPostRenderOptions {
|
||||
onReady?: () => void
|
||||
tikz?: boolean
|
||||
mermaid?: () => boolean
|
||||
shikiji?: () => boolean
|
||||
images?: () => string | null | undefined
|
||||
triggers?: Ref<unknown>[]
|
||||
}
|
||||
|
||||
export const useMarkdownPostRender = (
|
||||
contentRef: Ref<unknown>,
|
||||
scopeSelector: () => string,
|
||||
options: MarkdownPostRenderOptions = {}
|
||||
) => {
|
||||
const sources = [contentRef, ...(options.triggers ?? [])]
|
||||
|
||||
watch(
|
||||
sources,
|
||||
async () => {
|
||||
if (!toValue(contentRef)) return
|
||||
await nextTick()
|
||||
options.onReady?.()
|
||||
|
||||
const scope = scopeSelector()
|
||||
|
||||
if (options.tikz) {
|
||||
void runTikz(`${scope} .tikz`)
|
||||
}
|
||||
|
||||
if (options.mermaid?.()) {
|
||||
runMermaid(`${scope} .mermaid`)
|
||||
}
|
||||
|
||||
if (options.shikiji?.()) {
|
||||
void useShikiji()
|
||||
}
|
||||
|
||||
const imagesSha = options.images?.()
|
||||
if (imagesSha) {
|
||||
useImages(imagesSha)
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user