Skip to content

Commit

Permalink
Update docs vuese markdown render replace with parser and markdown-re…
Browse files Browse the repository at this point in the history
…nder
  • Loading branch information
akhuoa committed Mar 6, 2024
1 parent bc0c9c1 commit 52f0fad
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
6 changes: 4 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"@vitejs/plugin-vue": "^4.6.2",
"@vue/compiler-sfc": "^3.3.13",
"@vuese/cli": "^2.14.3",
"@vuese/markdown-render": "^2.11.3",
"@vuese/parser": "^2.10.3",
"auto-changelog": "^2.4.0",
"babel-eslint": "^10.1.0",
"babel-plugin-component": "^1.1.1",
Expand Down
51 changes: 41 additions & 10 deletions vuese-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,57 @@
* so that Vitepress dev will refresh the docs for components
*/

import fs from 'fs'
import path from 'path'
import chokidar from 'chokidar'
import { exec } from 'child_process'
import { parser } from '@vuese/parser'
import { Render } from '@vuese/markdown-render'

const componentsDir = 'src/components'
const components = ['FlatmapVuer.vue', 'MultiFlatmapVuer.vue']

const outputDir = 'docs/components'

const watcher = chokidar.watch(components, {
cwd: componentsDir,
ignoreInitial: true,
})

watcher.on('change', (path) => {
console.log(`The component ${path} has changed!`)
function generateMarkdown(file) {
const fileWithPath = `${componentsDir}/${file}`
const fileContent = fs.readFileSync(fileWithPath, 'utf-8')

try {
const parserResult = parser(fileContent)
const r = new Render(parserResult)
const renderResult = r.render()
const markdownResult = r.renderMarkdown()
const markdownContent = markdownResult.content
const componentName = path.basename(fileWithPath, '.vue')

exec('npm run vuese-gen', (error, stdout, stderr) => {
if (error) {
console.error(`Error executing vuese command: ${error}`)
return
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir)
}
console.log(`Vuese generated ${stdout}`)
})

fs.writeFile(`${outputDir}/${componentName}.md`, markdownContent, (err) => {
if (err) {
console.error(`Error writing markdown file for ${componentName}`, err)
} else {
console.log(`Markdown file for ${componentName} is generated!`)
}
})
} catch(e) {
console.error(e)
}
}

// Run on first load
components.forEach((component) => {
console.log(`Write markdown file for ${component} on first load.`)
generateMarkdown(component)
})

// Run on file change
watcher.on('change', (file) => {
console.log(`The component ${file} has changed!`)
generateMarkdown(file)
})

0 comments on commit 52f0fad

Please sign in to comment.