-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
doc: add in guide generation using remark #5408
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
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,32 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Node.js __VERSION__ Manual & Guide</title> | ||
| <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic"> | ||
| <link rel="stylesheet" href="assets/style.css"> | ||
| <link rel="stylesheet" href="assets/sh.css"> | ||
| <link rel="canonical" href="https://nodejs.org/guides/__FILENAME__.html"> | ||
| </head> | ||
| <body class="alt apidoc" id="api-section-__FILENAME__"> | ||
| <div id="content" class="clearfix"> | ||
| <div id="column2" class="interior"> | ||
| <div id="intro" class="interior"> | ||
| <a href="/" title="Go back to the home page"> | ||
| Node.js (1) | ||
| </a> | ||
| </div> | ||
| __GTOC__ | ||
| </div> | ||
|
|
||
| <div id="column1" data-id="__ID__" class="interior"> | ||
| <div id="apicontent"> | ||
| __CONTENT__ | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <script src="assets/sh_main.js"></script> | ||
| <script src="assets/sh_javascript.min.js"></script> | ||
| <script>highlight(undefined, undefined, 'pre');</script> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| const path = require('path'); | ||
| const fs = require('fs'); | ||
|
|
||
| const remark = require('remark'); | ||
| const html = require('remark-html'); | ||
| const toc = require('remark-toc'); | ||
| const hljs = require('remark-highlight.js'); | ||
|
|
||
| const processor = remark() | ||
| .use(toc) | ||
| .use(html) | ||
| .use(hljs) | ||
| const remarkOptions = { | ||
| yaml: true, | ||
| bullet: '*' | ||
| } | ||
|
|
||
| const guidesDir = path.join(__dirname, '../../', 'doc', 'guides'); | ||
| const outputDir = path.join(__dirname, '../../', 'out', 'doc', 'guides'); | ||
| const templateFile = path.join(__dirname, '../../', 'doc', 'template-guide.html'); | ||
| const template = fs.readFileSync(templateFile, { encoding: 'utf8' }) | ||
|
|
||
| fs.readdir(guidesDir, function(err, files) { | ||
| if (err) { | ||
| throw err; | ||
| } | ||
| files.forEach(function(fileName) { | ||
| const pageSlug = path.basename(fileName, '.md'); | ||
| fs.readFile(path.join(guidesDir, fileName), { encoding: 'utf8' }, function(err, content) { | ||
|
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. Don't ignore |
||
| var mdast = processor.parse(content, remarkOptions); | ||
| mdast = processor.run(mdast); | ||
| const html = processor.stringify(mdast, remarkOptions); | ||
| // Locate YAML front matter | ||
| if (mdast.children[0] && mdast.children[0].type === 'yaml') { | ||
|
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. Why not use a YAML parser such as js-yaml? Currently this code wouldn't support the full YAML specification, which could be confusing for people expecting to be able to write true YAML within the docs. |
||
| // Found YAML | ||
| var yamlData = {} | ||
| mdast.children[0].value.split('\n').forEach(function(line) { | ||
| const keyValue = line.match(/(\w+):\s?(.+)/) | ||
|
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. I'm thinking that at some point we'll want to have comments in the YAML. We should probably handle the line beginning with 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. This also won't currently match all of the values allowed for a key with YAML. Things can be escaped, in quotes, have dashes, etc. 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. I was trying to reduce external dependencies. But you made a good point. |
||
| yamlData[keyValue[1]] = keyValue[2] | ||
| }) | ||
| // console.log(yamlData) | ||
| } | ||
| var output = template.replace(/__VERSION__/g, process.version); | ||
| output = output.replace(/__CONTENT__/g, html); | ||
| output = output.replace(/__FILENAME__/g, pageSlug); | ||
| fs.writeFile(path.join(outputDir, pageSlug + '.html'), output, function(err) { | ||
| if (err) { | ||
| throw err; | ||
| } | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
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.
Shouldn't this file be running in strict mode?