From ccb5b194f8f3e1608ccbaedb7ed0c52346b8e1e0 Mon Sep 17 00:00:00 2001 From: tedjmzhang Date: Mon, 3 Jun 2024 14:26:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 4 +- src/api-examples.md | 49 -------------------- src/frontend/library/commander/commander.js | 1 + src/frontend/library/commander/commander.md | 6 +-- src/frontend/library/gogocode/index.md | 2 +- src/frontend/library/inquirer/index.md | 2 +- src/hooks.js | 51 +++++++++++++++++++++ src/index.md | 2 - src/markdown-examples.md | 1 - 9 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 src/api-examples.md mode change 100644 => 100755 src/frontend/library/commander/commander.js create mode 100644 src/hooks.js delete mode 100644 src/markdown-examples.md diff --git a/package.json b/package.json index b24badb..0c2251d 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ }, "scripts": { "docs:dev": "vitepress dev", - "docs:build": "vitepress build", + "docs:build": "node ./src/hooks.js && vitepress build", "docs:preview": "vitepress preview" }, "dependencies": { @@ -16,7 +16,7 @@ "@metalsmith/layouts": "^2.7.0", "@metalsmith/markdown": "^1.10.0", "@metalsmith/permalinks": "^3.0.1", - "chalk": "^5.3.0", + "chalk": "^5.3.0", "commander": "^12.1.0", "gogocode": "^1.0.55", "handlebars": "^4.7.8", diff --git a/src/api-examples.md b/src/api-examples.md deleted file mode 100644 index 1e1a60c..0000000 --- a/src/api-examples.md +++ /dev/null @@ -1,49 +0,0 @@ ---- -outline: deep ---- - -# Runtime API Examples - -This page demonstrates1 usage of some of the runtime APIs provided by VitePress1. - -The main `useData()` API can be used to access site, theme, and page data for the current page. It works in both `.md` and `.vue` files: - -```md - - -## Results - -### Theme Data -
{{ theme }}
- -### Page Data -
{{ page }}
- -### Page Frontmatter -
{{ frontmatter }}
-``` - - - -## Results - -### Theme Data -
{{ theme }}
- -### Page Data -
{{ page }}
- -### Page Frontmatter -
{{ frontmatter }}
- -## More - -Check out the documentation for the [full list of runtime APIs](https://vitepress.dev/reference/runtime-api#usedata). diff --git a/src/frontend/library/commander/commander.js b/src/frontend/library/commander/commander.js old mode 100644 new mode 100755 index ed25715..cc39096 --- a/src/frontend/library/commander/commander.js +++ b/src/frontend/library/commander/commander.js @@ -1,3 +1,4 @@ +#!/usr/bin/env node const { program } = require('commander'); diff --git a/src/frontend/library/commander/commander.md b/src/frontend/library/commander/commander.md index 22f550b..f64a358 100644 --- a/src/frontend/library/commander/commander.md +++ b/src/frontend/library/commander/commander.md @@ -1,7 +1,7 @@ # commander.js ## 名词概念 -1. 选项(options):终端传递的以`-`或者`--`开头的参数。 +1. 选项(option):终端传递的以`-`或者`--`开头的参数。 2. 命令(command):终端可以调用的命令,例如git add .中的add 3. 子命令(sub-command):一个独立的文件,名称默认为`主命令文件名称-子命令名称` 4. 选项参数,命令参数:跟在选项或者命令之后的参数,<>表示必选,[]表示可选,终端没出现该选项名称或者命令名称时候,默认为undefined @@ -29,7 +29,7 @@ node ./commander.js start clone placeA placeB // 会调用同级目录下面的c ``` const { program } = require('commander'); // program是一个全局的Command实例 -program.options("-s --search") // 返回全局Command实例 +program.option("-s --search") // 返回全局Command实例 .command('commandA") // 创建一个CommandA实例 .command('commandB") // 在CommandA实例下面创建一个新的CommandB实例 @@ -41,6 +41,6 @@ program.command('commandC') //在全局Command下面创建一个新的CommandC [具体见官方文档](https://github.com/tj/commander.js/blob/HEAD/Readme_zh-CN.md) - option - command -- action 参数为命令的所有参数,外加上解析出来的options和command实例 +- action 参数为命令的所有参数,外加上解析出来的option和command实例 - version - hook \ No newline at end of file diff --git a/src/frontend/library/gogocode/index.md b/src/frontend/library/gogocode/index.md index 38d1765..b0cf600 100644 --- a/src/frontend/library/gogocode/index.md +++ b/src/frontend/library/gogocode/index.md @@ -4,7 +4,7 @@ let ast = gogocode(string) // 生成gogoCode的ast实例 ## 实例方法: 全部返回的都是gogocode实例,只不过他们的match不一样而已,返回值是一个对象,但是有each方法,返回匹配的多个值,直接调用match只会返回匹配的第一个值(改值是一个对象,对象的key是$_$name里面的name) -- find: 匹配某格式文本,并且将其命名,ast.find('const $_$key = $_$value'), 可以使用.match或者(.each(item,index => item.match)index为1是表示第一个)获取第一个匹配的,如果有多个匹配,只能.each(item => item.match) +- find: 匹配某格式文本,并且将其命名,ast.find('const $\_$key = $\_$value'), 可以使用.match或者(.each(item,index => item.match)index为1是表示第一个)获取第一个匹配的,如果有多个匹配,只能.each(item => item.match) - replaceBy: find之后直接替换 diff --git a/src/frontend/library/inquirer/index.md b/src/frontend/library/inquirer/index.md index 376f991..1ba5e92 100644 --- a/src/frontend/library/inquirer/index.md +++ b/src/frontend/library/inquirer/index.md @@ -1,3 +1,3 @@ # inquirer -命令行交互库 \ No newline at end of file +命令行交互库, 统一所有交互,然后再后面的then作为返回值处理 \ No newline at end of file diff --git a/src/hooks.js b/src/hooks.js new file mode 100644 index 0000000..a269fc5 --- /dev/null +++ b/src/hooks.js @@ -0,0 +1,51 @@ +#!/usr/bin/env node + +let { program } = require("commander"); +let { readFileSync, readdirSync, writeFileSync } = require("fs"); +let path = require('path'); + +program.command('prebuild').description('发布之前执行的hook') + .action(() => { + appendJSToMD(__dirname) + }) + + +function appendJSToMD(dirPath) { + console.log(__dirname) + let currentDirName = path.basename(dirPath) + let dirInfoList = readdirSync(dirPath, { withFileTypes: true }); + // 该文件夹下面有index.js 或者dirname.js 同时该文件下面有index.md + let files = dirInfoList.filter(item => item.isFile()); + let dirs = dirInfoList.filter(item => item.isDirectory()); + let origin = null, destination = null; + files.forEach(item => { + if (['index.md', `${currentDirName}.md`].includes(item.name)) { + destination = item + return; + } + if (['index.js', `${currentDirName}.js`, 'index.mjs', `${currentDirName}.mjs`].includes(item.name)) { + origin = item; + return; + } + }) + + if (origin && destination) { + let originText = readFileSync(path.resolve(dirPath, origin.name)); + writeFileSync(path.resolve(dirPath, "index-source.md"), ` +\`\`\` + ${originText} +\`\`\` + `) + } + + dirs.forEach(item => { + appendJSToMD(path.resolve(dirPath, item.name)) + }) + + + + console.log('dir', dirInfoList); +} + + +appendJSToMD(__dirname); \ No newline at end of file diff --git a/src/index.md b/src/index.md index 2b219d6..798c888 100644 --- a/src/index.md +++ b/src/index.md @@ -9,10 +9,8 @@ hero: actions: - theme: brand1 text: Markdown Examples - link: /markdown-examples - theme: alt text: API Examples - link: /api-examples features: - title: Feature A1 diff --git a/src/markdown-examples.md b/src/markdown-examples.md deleted file mode 100644 index b726568..0000000 --- a/src/markdown-examples.md +++ /dev/null @@ -1 +0,0 @@ -examples \ No newline at end of file