Skip to content

Commit

Permalink
chore: tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo committed Jan 7, 2025
1 parent 6ab901e commit 41ac6b4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/1.示例/normal.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<div class="small">hello world</div>
</div>

<script type="config">
{
"jsLib": [],
"cssLib": []
}
</script>

<script lang="ts">
const a = 'hello world'
const app = document.querySelector('#app')
Expand Down
1 change: 1 addition & 0 deletions docs/1.示例/示例文章7.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ permalink: /article/5xbhl125/

```json
{
"jsLib": [],
"cssLib": []
}
```
Expand Down
3 changes: 3 additions & 0 deletions plugins/plugin-md-power/src/node/demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import type { App } from 'vuepress'
import type { Markdown } from 'vuepress/markdown'
import { demoContainer, demoEmbed } from './demo.js'
import { createDemoRender } from './watcher.js'

export function demoPlugin(app: App, md: Markdown) {
createDemoRender()
demoEmbed(app, md)
demoContainer(app, md)
}

export * from './extendPage.js'
export * from './watcher.js'
3 changes: 3 additions & 0 deletions plugins/plugin-md-power/src/node/demo/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import path from 'node:path'
import { compileScript, compileStyle } from './supports/compiler.js'
import { findFile, readFileSync, writeFileSync } from './supports/file.js'
import { insertSetupScript } from './supports/insertScript.js'
import { checkDemoRender, markDemoRender } from './watcher.js'

interface NormalCode {
html?: string
Expand Down Expand Up @@ -66,6 +67,7 @@ function codeToHtml(md: Markdown, source: NormalCode, info: string): string {
}

async function compileCode(code: NormalCode, output: string) {
markDemoRender()
const res = { jsLib: [], cssLib: [], script: '', css: '', html: '' }
if (!fs.existsSync(output))
writeFileSync(output, `import { ref } from "vue"\nexport default ref(${JSON.stringify(res, null, 2)})`)
Expand All @@ -90,6 +92,7 @@ async function compileCode(code: NormalCode, output: string) {
}

writeFileSync(output, `import { ref } from "vue"\nexport default ref(${JSON.stringify(res, null, 2)})`)
checkDemoRender()
}

export function normalEmbed(
Expand Down
35 changes: 35 additions & 0 deletions plugins/plugin-md-power/src/node/demo/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 消除异步编译 demo 代码 与 markdown 同步 render 的时间差问题
* 确保 在 vuepress onPrepared 阶段完成所有 demo 代码的编译与输出
*/
let renderDone: null | ((...args: any[]) => void) = null
let renderCount = 0
let renderPromise!: Promise<void>

export function createDemoRender() {
renderPromise = new Promise((resolve) => {
renderDone = resolve
})
}

export async function waitDemoRender() {
if (renderCount === 0) {
renderDone?.()
renderDone = null
}
await renderPromise
}

export function markDemoRender() {
renderCount++
}

export function checkDemoRender() {
if (renderCount > 0) {
renderCount--
}
if (renderCount === 0) {
renderDone?.()
renderDone = null
}
}
7 changes: 6 additions & 1 deletion plugins/plugin-md-power/src/node/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { MarkdownPowerPluginOptions } from '../shared/index.js'
import { addViteOptimizeDepsInclude } from '@vuepress/helper'
import { isPackageExists } from 'local-pkg'
import { containerPlugin } from './container/index.js'
import { demoPlugin, extendsPageWithDemo } from './demo/index.js'
import { demoPlugin, extendsPageWithDemo, waitDemoRender } from './demo/index.js'
import { embedSyntaxPlugin } from './embed/index.js'
import { docsTitlePlugin } from './enhance/docsTitle.js'
import { imageSizePlugin } from './enhance/imageSize.js'
Expand Down Expand Up @@ -54,6 +54,11 @@ export function markdownPowerPlugin(
await imageSizePlugin(app, md, options.imageSize)
},

onPrepared: async () => {
if (options.demo)
await waitDemoRender()
},

extendsPage: (page) => {
if (options.demo)
extendsPageWithDemo(page)
Expand Down

0 comments on commit 41ac6b4

Please sign in to comment.