Skip to content

Commit

Permalink
feat(comment): add a ability to add custom met data and fix a bug whe…
Browse files Browse the repository at this point in the history
…n parsing index.js files

#2
  • Loading branch information
ph1p committed Nov 30, 2018
1 parent d4c3bf1 commit 30ec64e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,23 @@ module.exports = {
};
```

## @vuepress comment block

You can add custom meta data to your pages
Simply add:

```javascript
/*
* @vuepress
* ---
* title: You Custom Title
* ---
*/
```

More inromation: https://vuepress.vuejs.org/guide/markdown.html#front-matter


## Example

The `./example` folder includes a full working vuepress-jsdoc example.
Expand All @@ -114,4 +131,4 @@ yarn run build

- [ ] Update description README.md
- [ ] Custom README.md
- [ ] Custom meta data
- [x] Custom meta data
22 changes: 22 additions & 0 deletions helpers/commentParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fm = require('front-matter');

module.exports = fileContent => {
try {
let allCommentBlocks = fileContent.match(/\/\*[\s\S]*?\*\/|([^:]|^)\/\/.*$/g);
const vuepressBlock = allCommentBlocks.filter(block => {
return block.split('\n').filter(line => line.indexOf('@vuepress') >= 0).length;
})[0];

return fm(
vuepressBlock
.replace(/\n /g, '\n')
.replace('/*', '')
.replace('*/', '')
.replace(/@vuepress/g, '')
.replace(/\*\s?/g, '')
.trim()
);
} catch (e) {
return '';
}
};
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const vuedoc = require('@vuedoc/md');
const rimraf = require('rimraf');

const vueSidebar = require('./helpers/vueSidebar');
const parseVuepressComment = require('./helpers/commentParser');

const fileTree = [];

Expand Down Expand Up @@ -93,7 +94,12 @@ const readFiles = async (folder, depth = 0, tree) => {
await asyncForEach(files, async file => {
const stat = await fs.lstat(`${folder}/${file}`);

const fileName = getFilename(file);
let fileName = getFilename(file);

// prefix index with unserscore, the generated index.html comes from vuepress
if (fileName === 'index') {
fileName = '_index';
}

if (stat.isDirectory(folder)) {
// check file length and skip empty folders
Expand All @@ -116,7 +122,7 @@ const readFiles = async (folder, depth = 0, tree) => {
else {
// check if extension is correct
if (checkExtension(file, extensions)) {
const fileData = await fs.readFile(`${folder}/${file}`);
const fileData = await fs.readFile(`${folder}/${file}`, 'utf8');
let mdFileData = '';

if (/\.vue$/.test(file)) {
Expand All @@ -135,9 +141,11 @@ const readFiles = async (folder, depth = 0, tree) => {
}

if (mdFileData !== '') {
const { frontmatter } = parseVuepressComment(fileData);

console.log('write file', `${folderPath}/${fileName}.md`);

await fs.writeFile(`${folderPath}/${fileName}.md`, `---\ntitle: ${fileName}\n---\n${mdFileData}`);
await fs.writeFile(`${folderPath}/${fileName}.md`, `---\n${frontmatter || `title: ${fileName}`}\n---\n${mdFileData}`);

tree.push({
name: fileName,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@vuedoc/md": "^1.5.0",
"bluebird": "^3.5.3",
"cross-env": "^5.2.0",
"front-matter": "^3.0.1",
"fs.promised": "^3.0.0",
"husky": "^1.2.0",
"jsdoc-to-markdown": "^4.0.1",
Expand All @@ -65,7 +66,7 @@
"lint-staged": "^8.1.0"
},
"lint-staged": {
"./src/**/*.{js,jsx,css,scss,json,ts,vue}": [
"**/*.{js,jsx,css,scss,json,ts,vue}": [
"prettier --write",
"git add"
]
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,13 @@ fragment-cache@^0.2.1:
dependencies:
map-cache "^0.2.2"

front-matter@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/front-matter/-/front-matter-3.0.1.tgz#9ed0a8e2771b1367c4d8809b2f91c528b9b04d0c"
integrity sha512-iCHZ7RZGE36uG58iIWp8zrhDi9BZjlDiRj7aRcGm45EIqrbK+u4KTAmRKLG3FOaVkFhZI5/29SUo7sMLzlQkcA==
dependencies:
js-yaml "^3.10.0"

fs-minipass@^1.2.5:
version "1.2.5"
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d"
Expand Down Expand Up @@ -2686,7 +2693,7 @@ js-tokens@^3.0.2:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
integrity sha1-mGbfOVECEw449/mWvOtlRDIJwls=

js-yaml@^3.7.0, js-yaml@^3.9.0:
js-yaml@^3.10.0, js-yaml@^3.7.0, js-yaml@^3.9.0:
version "3.12.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.12.0.tgz#eaed656ec8344f10f527c6bfa1b6e2244de167d1"
integrity sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A==
Expand Down

0 comments on commit 30ec64e

Please sign in to comment.