-
-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate index file #46
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env node | ||
|
||
const { docTitle } = require('../constants'); | ||
const chapterHelper = require('../src/helper/chapterHelper'); | ||
const { readFileSync, writeFile } = require('fs'); | ||
const { resolve } = require('path'); | ||
const jsyaml = require('js-yaml'); | ||
const fileProcessor = require('./fileProcessor'); | ||
|
||
const loadedOutline = jsyaml.safeLoad(readFileSync(`${__dirname}/../config/outline.yaml`, 'utf8')); | ||
|
||
const index = `# ${docTitle}\n\n${loadedOutline.map((chapter) => chapterHelper.processChapter(chapter, fileProcessor)).join('\n\n')}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
writeFile(resolve(__dirname, '../src/pages/docs/index.md'), index, error => error && console.warn(error)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it really necessary to generate a Markdown file? Can't we use directly the existing YAML file as a source for Gatsby? https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-yaml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
module.exports = Object.freeze({ | ||
docPagesDirectory: `${__dirname}/src/pages/docs`, | ||
docTitle: 'API Platform documentation', | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const chapterHelper = { | ||
processChapterItems(chapter, fileProcessor) { | ||
const items = chapter.items.map(file => fileProcessor.processFile(chapter.path, file.id)[0]); | ||
|
||
return items.map((item, index) => this.processChapterItem(chapter.path, item, index)).join(''); | ||
}, | ||
processChapterItem(path, item, index) { | ||
return `${index + 1}.${(index + 1).toString().length !== 2 ? ' ' : ' '}[${item.title}](${path}/${item.id}.md)\n${this.processItemAnchors(item, path) ? this.processItemAnchors(item, path) : ''}`; | ||
}, | ||
processItemAnchors(item, path, depth = 2) { | ||
if (!item.anchors) { | ||
return; | ||
} | ||
|
||
return item.anchors.map((anchor, index) => this.processItemAnchor(anchor, path, index, depth)).join(''); | ||
}, | ||
processItemAnchor(anchor, path, index, depth) { | ||
return `${' '.repeat(depth)}${index + 1}.${(index + 1).toString().length !== 2 ? ' ' : ' '}[${anchor.title}](${path}#${anchor.id})\n${anchor.anchors ? this.processItemAnchors(anchor, path , ++depth) : ''}`; | ||
}, | ||
processChapter(chapter, fileProcessor) { | ||
return `## ${chapter.title}\n\n${this.processChapterItems(chapter, fileProcessor)}`; | ||
}, | ||
}; | ||
|
||
module.exports = chapterHelper; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you use ES6 modules instead? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{ processChapter } = require('../src/helper/chapterHelper');