-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add vite markdown plugin (#2650)
将 markdown 相关插件独立为 @nutui/vite-plugins 子包。 将 demo 拆分为单独的文件,中英文文档内容由 demo 代码直接生成。
- Loading branch information
Showing
26 changed files
with
939 additions
and
785 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
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,43 @@ | ||
{ | ||
"name": "@nutui/vite-plugins", | ||
"version": "1.0.0", | ||
"private": "true", | ||
"type": "module", | ||
"description": "", | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"./markdown": { | ||
"import": "./dist/markdown.js", | ||
"require": "./dist/markdown.cjs", | ||
"types": "./dist/markdown.d.ts" | ||
} | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "dist/index.js", | ||
"types": "./dist/index.d.ts", | ||
"files": ["dist"], | ||
"scripts": { | ||
"prepare": "pnpm build", | ||
"dev": "tsup --watch", | ||
"build": "tsup" | ||
}, | ||
"author": "jdf2e", | ||
"license": "MIT", | ||
"dependencies": { | ||
"lzutf8": "^0.6.3", | ||
"markdown-it-container": "^3.0.0", | ||
"unplugin-vue-markdown": "^0.24.3", | ||
"highlight.js": "^11.9.0", | ||
"fs-extra": "^11.1.1" | ||
}, | ||
"devDependencies": { | ||
"tsup": "^7.2.0", | ||
"typescript": "^5.0.4", | ||
"@types/markdown-it-container": "^2.0.8", | ||
"@types/fs-extra": "^11.0.3" | ||
} | ||
} |
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,2 @@ | ||
export * from './markdown'; | ||
export { default as markdown } from './markdown'; |
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,90 @@ | ||
import Markdown from 'unplugin-vue-markdown/vite'; | ||
import MarkdownIt from 'markdown-it-container'; | ||
import hljs from 'highlight.js'; | ||
import LZUTF8 from 'lzutf8'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { Plugin } from 'vite'; | ||
|
||
function compressText(str: string): any { | ||
// @ts-ignore | ||
return LZUTF8.encodeBase64(LZUTF8.compress(str, { outputEncoding: 'ByteArray' })); | ||
} | ||
|
||
interface MarkdownOptions { | ||
docRoot: string; | ||
} | ||
|
||
const fileRegex = /\.md$/; | ||
|
||
const TransformMarkdownDemo = (options: MarkdownOptions): Plugin => { | ||
return { | ||
name: 'nutui-transform-markdown-demo', | ||
enforce: 'pre', | ||
transform(src, id) { | ||
if (fileRegex.test(id)) { | ||
return { | ||
code: src.replace(/> demo: ([0-9a-z .]*)[\n|\r\n]/g, (_match, $1: string) => { | ||
const [left, right] = $1.split(' '); | ||
let code = ''; | ||
try { | ||
code = fs.readFileSync(path.resolve(options.docRoot, left, 'demo', `${right}.vue`), 'utf-8'); | ||
} catch (err) { | ||
code = | ||
'[@nutui/vite-plugins] File not found: ' + path.resolve(options.docRoot, left, 'demo', `${right}.vue`); | ||
console.warn(code); | ||
} | ||
return code | ||
? `:::demo | ||
\`\`\`vue | ||
${code} | ||
\`\`\` | ||
:::\n` | ||
: ''; | ||
}), | ||
map: null // 如果可行将提供 source map | ||
}; | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export default function markdown(options: MarkdownOptions) { | ||
return [ | ||
TransformMarkdownDemo(options), | ||
Markdown({ | ||
markdownItOptions: { | ||
typographer: false, | ||
highlight: function (str, lang) { | ||
if (lang && (lang === 'vue' || hljs.getLanguage(lang))) { | ||
return hljs.highlight(str, { | ||
language: lang === 'vue' ? 'html' : lang | ||
}).value; | ||
} | ||
return ''; | ||
} | ||
}, | ||
markdownItSetup(md) { | ||
md.use(MarkdownIt, 'demo', { | ||
validate: function (params: any) { | ||
return params.match(/^demo\s*(.*)$/); | ||
}, | ||
render: function (tokens: any, idx: any) { | ||
const m = tokens[idx].info.trim().match(/^demo\s*(.*)$/); | ||
if (tokens[idx].nesting === 1) { | ||
// opening tag | ||
const contentHtml = compressText(tokens[idx + 1].content); | ||
return `<demo-block data-type="vue" data-value="${contentHtml}">` + md.utils.escapeHtml(m[1]) + '\n'; | ||
} else { | ||
// closing tag | ||
return '</demo-block>\n'; | ||
} | ||
} | ||
}); | ||
} | ||
}) | ||
]; | ||
} |
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,10 @@ | ||
import type { Options } from 'tsup'; | ||
|
||
export default <Options>{ | ||
entryPoints: ['src/*.ts'], | ||
clean: true, | ||
format: ['cjs', 'esm'], | ||
dts: true, | ||
shims: true, | ||
silent: true | ||
}; |
Oops, something went wrong.