Skip to content
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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/fileProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const HTML5Outline = require('h5o');
const { JSDOM } = require('jsdom');
const fileContentHelper = require('../src/lib/fileContentHelper');
const treeHelper = require('../src/lib/treeHelper');
const fileContentHelper = require('../src/helper/fileContentHelper');
const treeHelper = require('../src/helper/treeHelper');

function getHTML5Outline(fileContent) {
return HTML5Outline(new JSDOM(fileContent).window.document.body);
Expand Down
14 changes: 14 additions & 0 deletions bin/generate-index
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');
Copy link
Contributor Author

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');

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')}`;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chapterHelper.processChapter => processChapter


writeFile(resolve(__dirname, '../src/pages/docs/index.md'), index, error => error && console.warn(error));
Copy link
Member

Choose a reason for hiding this comment

The 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
gatsbyjs/gatsby#3195

1 change: 1 addition & 0 deletions bin/retrieve-documentation
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ git clone https://github.com/api-platform/docs.git src/pages/docs

bin/check-outline
bin/generate-nav
bin/generate-index
1 change: 1 addition & 0 deletions constants.js
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',
});
1 change: 1 addition & 0 deletions src/data/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const menu = [
{
text: 'docs',
rootPath: '/docs',
path: '/docs/index',
submenu: [
{
text: 'Distribution',
Expand Down
25 changes: 25 additions & 0 deletions src/helper/chapterHelper.js
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you use ES6 modules instead?

File renamed without changes.
File renamed without changes.
File renamed without changes.