| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f9f85a0 commit 9ef6e23
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,7 @@ const assert = require('assert'); | |||
| 5 | 5 | const fs = require('fs'); | |
| 6 | 6 | const path = require('path'); | |
| 7 | 7 | ||
| 8 | + const processIncludes = require('../../tools/doc/preprocess.js'); | ||
| 8 | 9 | const html = require('../../tools/doc/html.js'); | |
| 9 | 10 | ||
| 10 | 11 | // Test data is a list of objects with two properties. | |
@@ -53,30 +54,43 @@ const testData = [ | |||
| 53 | 54 | '<p>Describe <code>Something</code> in more detail here. ' + | |
| 54 | 55 | '</p>' | |
| 55 | 56 | }, | |
| 57 | + { | ||
| 58 | + file: path.join(common.fixturesDir, 'doc_with_includes.md'), | ||
| 59 | + html: '<!-- [start-include:doc_inc_1.md] -->' + | ||
| 60 | + '<p>Look <a href="doc_inc_2.html#doc_inc_2_foobar">here</a>!</p>' + | ||
| 61 | + '<!-- [end-include:doc_inc_1.md] -->' + | ||
| 62 | + '<!-- [start-include:doc_inc_2.md] -->' + | ||
| 63 | + '<h1>foobar<span><a class="mark" href="#doc_inc_2_foobar" ' + | ||
| 64 | + 'id="doc_inc_2_foobar">#</a></span></h1>' + | ||
| 65 | + '<p>I exist and am being linked to.</p>' + | ||
| 66 | + '<!-- [end-include:doc_inc_2.md] -->' | ||
| 67 | + }, | ||
| 56 | 68 | ]; | |
| 57 | 69 | ||
| 58 | 70 | testData.forEach(function(item) { | |
| 59 | 71 | // Normalize expected data by stripping whitespace | |
| 60 | 72 | const expected = item.html.replace(/\s/g, ''); | |
| 61 | 73 | ||
| 62 | - fs.readFile(item.file, 'utf8', common.mustCall(function(err, input) { | ||
| 74 | + fs.readFile(item.file, 'utf8', common.mustCall((err, input) => { | ||
| 63 | 75 | assert.ifError(err); | |
| 64 | - html( | ||
| 65 | - { | ||
| 66 | - input: input, | ||
| 67 | - filename: 'foo', | ||
| 68 | - template: 'doc/template.html', | ||
| 69 | - nodeVersion: process.version, | ||
| 70 | - }, | ||
| 76 | + processIncludes(item.file, input, common.mustCall((err, preprocessed) => { | ||
| 77 | + assert.ifError(err); | ||
| 71 | 78 | ||
| 72 | - common.mustCall(function(err, output) { | ||
| 73 | - assert.ifError(err); | ||
| 79 | + html( | ||
| 80 | + { | ||
| 81 | + input: preprocessed, | ||
| 82 | + filename: 'foo', | ||
| 83 | + template: 'doc/template.html', | ||
| 84 | + nodeVersion: process.version, | ||
| 85 | + }, | ||
| 86 | + common.mustCall((err, output) => { | ||
| 87 | + assert.ifError(err); | ||
| 74 | 88 | ||
| 75 | - const actual = output.replace(/\s/g, ''); | ||
| 76 | - // Assert that the input stripped of all whitespace contains the | ||
| 77 | - // expected list | ||
| 78 | - assert.notEqual(actual.indexOf(expected), -1); | ||
| 79 | - }) | ||
| 80 | - ); | ||
| 89 | + const actual = output.replace(/\s/g, ''); | ||
| 90 | + // Assert that the input stripped of all whitespace contains the | ||
| 91 | + // expected list | ||
| 92 | + assert.notEqual(actual.indexOf(expected), -1); | ||
| 93 | + })); | ||
| 94 | + })); | ||
| 81 | 95 | })); | |
| 82 | 96 | }); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + Look [here][]! | ||
| 2 | + | ||
| 3 | + [here]: doc_inc_2.html#doc_inc_2_foobar | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + # foobar | ||
| 2 | + | ||
| 3 | + I exist and am being linked to. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + @include doc_inc_1 | ||
| 2 | + @include doc_inc_2.md | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,15 +283,30 @@ function getSection(lexed) { | |||
| 283 | 283 | function buildToc(lexed, filename, cb) { | |
| 284 | 284 | var toc = []; | |
| 285 | 285 | var depth = 0; | |
| 286 | + | ||
| 287 | + const startIncludeRefRE = /^\s*<!-- \[start-include:(.+)\] -->\s*$/; | ||
| 288 | + const endIncludeRefRE = /^\s*<!-- \[end-include:(.+)\] -->\s*$/; | ||
| 289 | + const realFilenames = [filename]; | ||
| 290 | + | ||
| 286 | 291 | lexed.forEach(function(tok) { | |
| 292 | + // Keep track of the current filename along @include directives. | ||
| 293 | + if (tok.type === 'html') { | ||
| 294 | + let match; | ||
| 295 | + if ((match = tok.text.match(startIncludeRefRE)) !== null) | ||
| 296 | + realFilenames.unshift(match[1]); | ||
| 297 | + else if (tok.text.match(endIncludeRefRE)) | ||
| 298 | + realFilenames.shift(); | ||
| 299 | + } | ||
| 300 | + | ||
| 287 | 301 | if (tok.type !== 'heading') return; | |
| 288 | 302 | if (tok.depth - depth > 1) { | |
| 289 | 303 | return cb(new Error('Inappropriate heading level\n' + | |
| 290 | 304 | JSON.stringify(tok))); | |
| 291 | 305 | } | |
| 292 | 306 | ||
| 293 | 307 | depth = tok.depth; | |
| 294 | - var id = getId(filename + '_' + tok.text.trim()); | ||
| 308 | + const realFilename = path.basename(realFilenames[0], '.md'); | ||
| 309 | + const id = getId(realFilename + '_' + tok.text.trim()); | ||
| 295 | 310 | toc.push(new Array((depth - 1) * 2 + 1).join(' ') + | |
| 296 | 311 | '* <a href="#' + id + '">' + | |
| 297 | 312 | tok.text + '</a>'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,7 +48,11 @@ function processIncludes(inputFile, input, cb) { | |||
| 48 | 48 | if (errState) return; | |
| 49 | 49 | if (er) return cb(errState = er); | |
| 50 | 50 | incCount--; | |
| 51 | - includeData[fname] = inc; | ||
| 51 | + | ||
| 52 | + // Add comments to let the HTML generator know how the anchors for | ||
| 53 | + // headings should look like. | ||
| 54 | + includeData[fname] = `<!-- [start-include:${fname}] -->\n` + | ||
| 55 | + inc + `\n<!-- [end-include:${fname}] -->\n`; | ||
| 52 | 56 | input = input.split(include + '\n').join(includeData[fname] + '\n'); | |
| 53 | 57 | if (incCount === 0) { | |
| 54 | 58 | return cb(null, input); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments