Skip to content

Commit 7cf337c

Browse files
committed
feat: compile as template
Signed-off-by: Neko Ayaka <[email protected]>
1 parent a711404 commit 7cf337c

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@
3030
"@antfu/eslint-config": "^2.21.0",
3131
"@codemirror/view": "^6.28.4",
3232
"@types/node": "^20.14.10",
33+
"@vue/compiler-sfc": "^3.4.31",
3334
"builtin-modules": "^3.3.0",
3435
"eslint": "^8.57.0",
36+
"hast-util-to-html": "^9.0.1",
3537
"obsidian": "^1.5.7",
3638
"rehype-raw": "^7.0.0",
3739
"rehype-stringify": "^10.0.0",

pnpm-lock.yaml

Lines changed: 9 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { Plugin } from 'obsidian'
22
import type { EditorView, PluginValue, ViewUpdate } from '@codemirror/view'
33
import { ViewPlugin } from '@codemirror/view'
44
import { type App, createApp, defineComponent, h } from 'vue'
5+
import { compileTemplate } from '@vue/compiler-sfc'
56

67
import { unified } from 'unified'
78
import RemarkParse from 'remark-parse'
89
import RemarkRehype from 'remark-rehype'
910
import RehypeRaw from 'rehype-raw'
1011
import { remove } from 'unist-util-remove'
12+
import { toHtml } from 'hast-util-to-html'
1113

1214
function sleep(ms: number) {
1315
return new Promise(resolve => setTimeout(resolve, ms))
@@ -60,8 +62,26 @@ class VueViewPlugin implements PluginValue {
6062
.use(() => tree => remove(tree, (node, _, parent) => parent?.type === 'root' && node.type === 'text'))
6163
.run(parsedMarkdownAst)
6264

63-
// eslint-disable-next-line no-console
64-
console.log('transformedHast', transformedHast)
65+
let index = 0
66+
for (const node of transformedHast.children) {
67+
index++
68+
69+
const componentTemplateStr = toHtml(node)
70+
71+
const { code, errors } = compileTemplate({
72+
isProd: false,
73+
source: componentTemplateStr,
74+
filename: `some-${index}`,
75+
id: index.toString(),
76+
})
77+
if (errors.length) {
78+
console.error(errors)
79+
throw new Error('Failed to compile template')
80+
}
81+
82+
// eslint-disable-next-line no-console
83+
console.log(code)
84+
}
6585

6686
this.vueInstance?.unmount()
6787

0 commit comments

Comments
 (0)