-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import escapeHtml from 'escape-html' | ||
import prism from 'prismjs' | ||
let loadLanguages: any | ||
load(['markup', 'css', 'javascript']) | ||
function wrap(code: string, lang: string): string { | ||
if (lang === 'text') { | ||
code = escapeHtml(code) | ||
} | ||
return `<pre v-pre><code>${code}</code></pre>` | ||
} | ||
|
||
export const highlight = (str: string, lang: string) => { | ||
if (!lang) { | ||
return wrap(str, 'text') | ||
} | ||
lang = lang.toLowerCase() | ||
const rawLang = lang | ||
if (lang === 'vue' || lang === 'html') { | ||
lang = 'markup' | ||
} | ||
if (lang === 'md') { | ||
lang = 'markdown' | ||
} | ||
if (lang === 'ts') { | ||
lang = 'typescript' | ||
} | ||
if (lang === 'py') { | ||
lang = 'python' | ||
} | ||
if (!prism.languages[lang]) { | ||
try { | ||
load([lang]) | ||
} catch { | ||
console.warn( | ||
`[vitepress] Syntax highlight for language "${lang}" is not supported.` | ||
) | ||
} | ||
} | ||
if (prism.languages[lang]) { | ||
const code = prism.highlight(str, prism.languages[lang], lang) | ||
return wrap(code, rawLang) | ||
} | ||
return wrap(str, 'text') | ||
} | ||
|
||
async function load(lans: string[]) { | ||
if (!loadLanguages) { | ||
const mod = await import('prismjs/components/index.js') | ||
loadLanguages = mod.default | ||
loadLanguages(lans) | ||
} else { | ||
loadLanguages(lans) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import MarkdownIt from 'markdown-it' | ||
import mdContainer from 'markdown-it-container' | ||
import type Token from 'markdown-it/lib/token' | ||
import type Renderer from 'markdown-it/lib/renderer' | ||
import path from 'path' | ||
import fs from 'fs' | ||
|
||
import { highlight } from './highlight' | ||
|
||
const localMd = MarkdownIt() | ||
|
||
interface ContainerOpts { | ||
marker?: string | undefined | ||
validate?(params: string): boolean | ||
render?( | ||
tokens: Token[], | ||
index: number, | ||
options: any, | ||
env: any, | ||
self: Renderer | ||
): string | ||
} | ||
|
||
export const mdPlugin = (md: MarkdownIt) => { | ||
md.use(mdContainer, 'div', { | ||
validate(params) { | ||
return !!params.trim().match(/^demo\s*(.*)$/) | ||
}, | ||
render(tokens, idx) { | ||
/** | ||
:::demo Use `type`, `plain`, `round` and `circle` to define Button's style. | ||
button/basic | ||
::: | ||
*/ | ||
// console.log(tokens[idx].info, 'tokens, idx') | ||
|
||
const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/) | ||
if (tokens[idx].nesting === 1 /* means the tag is opening */) { | ||
// 拿到描述 Use `type`, `plain`, `round` and `circle` to define Button's style. | ||
const description = m && m.length > 1 ? m[1] : '' | ||
|
||
// 获取文件路径的 token | ||
const sourceFileToken = tokens[idx + 2] | ||
let source = '' | ||
// 文件路径 button/basic | ||
const sourceFile = sourceFileToken.children?.[0].content ?? '' | ||
|
||
if (sourceFileToken.type === 'inline') { | ||
// 根据路径读取 examples/button/basic.vue | ||
source = fs.readFileSync( | ||
path.resolve(__dirname, '../../examples', `${sourceFile}.vue`), | ||
'utf-8' | ||
) | ||
} | ||
if (!source) throw new Error(`Incorrect source file: ${sourceFile}`) | ||
|
||
// 将代码传给 Demo 组件,Demo 组件的封装下面讲 | ||
// highlight 的作用就是将代码包裹一下,根据传递的语言加载该语言的样式 | ||
return `<Demo source="${encodeURIComponent( | ||
highlight(source, 'vue') | ||
)}" path="${sourceFile}" raw-source="${encodeURIComponent( | ||
source | ||
)}" description="${encodeURIComponent(localMd.render(description))}">` | ||
} else { | ||
return `</Demo>` | ||
} | ||
} | ||
} as ContainerOpts) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<template> | ||
<button id="btn">按钮</button> | ||
</template> | ||
<script setup lang="ts"> | ||
import { onMounted, ref } from 'vue' | ||
const getElement = (cssSelector) => { | ||
const dom = document.querySelector(cssSelector) | ||
// Proxy 代理 | ||
const proxy = new Proxy(dom, { | ||
get(target, key: string) { | ||
if (!key.startsWith('wait')) { | ||
return Reflect.get(target, key) | ||
} | ||
return new Promise(resolve => { | ||
const eventName = key.replace('wait', '').toLowerCase() | ||
dom.addEventListener(eventName, resolve, { once: true }) | ||
}) | ||
} | ||
}) | ||
return proxy | ||
} | ||
onMounted(() => { | ||
(async () => { | ||
const btn = getElement('#btn') | ||
while (1) { | ||
await btn.waitClick; | ||
console.log('click') | ||
} | ||
})() | ||
}) | ||
</script> | ||
<style></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# 以同步方式实现事件监听 | ||
|
||
```js | ||
const getElement = (cssSelector) => { | ||
const dom = document.querySelector(cssSelector) | ||
// Proxy 代理 | ||
const proxy = new Proxy(dom, { | ||
get(target, key) { | ||
if(!key.startsWith('wait')) { | ||
return Reflect.get(target, key) | ||
} | ||
return new Promise(resolve => { | ||
const eventName = key.replace('wait', '').toLowerCase() | ||
|
||
dom.addEventListener(eventName, resolve, { once: true }) | ||
}) | ||
} | ||
}) | ||
return proxy | ||
} | ||
(async () => { | ||
const btn = getElement('#btn') | ||
while(1) { | ||
await btn.waitClick; | ||
console.log('click') | ||
} | ||
})() | ||
|
||
``` | ||
### 示例 | ||
|
||
<script setup> | ||
import AsyncAddlistener from '../code/demo/AsyncAddlistener.vue' | ||
</script> | ||
<AsyncAddlistener /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.