import '../common/index.mjs'; import * as fixtures from '../common/fixtures.mjs'; import assert from 'assert'; import { readFileSync } from 'fs'; import * as html from '../../tools/doc/html.mjs'; import { replaceLinks } from '../../tools/doc/markdown.mjs'; import { rehypeRaw, rehypeStringify, remarkParse, remarkRehype, unified, } from '../../tools/doc/deps.mjs'; // Test links mapper is an object of the following structure: // { // [filename]: { // [link definition identifier]: [url to the linked resource] // } // } const testLinksMapper = { 'foo': { 'command line options': 'cli.html#cli-options', 'web server': 'example.html', }, }; function toHTML({ input, filename, nodeVersion, versions }) { const content = unified() .use(replaceLinks, { filename, linksMapper: testLinksMapper }) .use(remarkParse) .use(html.firstHeader) .use(html.preprocessText, { nodeVersion }) .use(html.preprocessElements, { filename }) .use(html.buildToc, { filename, apilinks: {} }) .use(remarkRehype, { allowDangerousHtml: true }) .use(rehypeRaw) .use(rehypeStringify) .processSync(input); return html.toHTML({ input, content, filename, nodeVersion, versions }); } // Test data is a list of objects with two properties. // The file property is the file path. // The html property is some HTML which will be generated by the doctool. // This HTML will be stripped of all whitespace because we don't currently // have an HTML parser. const testData = [ { file: fixtures.path('order_of_end_tags_5873.md'), html: '

Static method: Buffer.from(array) ' + '# ' + '

' + '', }, { file: fixtures.path('doc_with_yaml.md'), html: '

Sample Markdown with YAML info' + '#' + '

' + '

Foobar#' + '

' + '
Added in: v1.0.0
' + '

Describe Foobar in more detail here.

' + '
' + '

Foobar II#' + '

' + '
' + '
History' + '' + '' + '' + '' + '
VersionChanges
v4.2.0

The error parameter can now be' + 'an arrow function.

v5.3.0, v4.2.0

Added in: v5.3.0, v4.2.0

' + '

Describe Foobar II in more detail here.' + 'fg(1)' + '

' + '

Deprecated thingy#' + '' + '

Added in: v1.0.0' + 'Deprecated since: v2.0.0

Describe ' + 'Deprecated thingy in more detail here.' + 'fg(1p)' + '

' + '

Something#' + '

' + ' ' + '

Describe Something in more detail here.

', }, { file: fixtures.path('sample_document.md'), html: '
  1. fish
  2. fish
' + '', }, { file: fixtures.path('altdocs.md'), html: '
  • 8.x', }, { file: fixtures.path('document_with_links.md'), html: '

    Usage and Example#' + '

    Usage#

    node \\[options\\] index.js' + '

    Please see the' + 'Command Line Optionsdocument for more information.

    ' + '

    ' + 'Example' + '#

    An example of a' + 'webserverwritten with Node.js which responds with' + '\'Hello, World!\':

    ' + '

    See also#

    Check' + 'out alsothis guide

    ', }, { file: fixtures.path('document_with_special_heading.md'), html: 'Sample markdown with special heading |', }, { file: fixtures.path('document_with_esm_and_cjs_code_snippet.md'), html: '<input class="js-flavor-toggle" type="checkbox" checked', }, { file: fixtures.path('document_with_cjs_and_esm_code_snippet.md'), html: '<input class="js-flavor-toggle" type="checkbox" aria-label', }, ]; const spaces = /\s/g; const versions = [ { num: '10.x', lts: true }, { num: '9.x' }, { num: '8.x' }, { num: '7.x' }, { num: '6.x' }, { num: '5.x' }, { num: '4.x' }, { num: '0.12.x' }, { num: '0.10.x' }]; testData.forEach(({ file, html }) => { // Normalize expected data by stripping whitespace. const expected = html.replace(spaces, ''); const input = readFileSync(file, 'utf8'); const output = toHTML({ input, filename: 'foo', nodeVersion: process.version, versions }); const actual = output.replace(spaces, ''); // Assert that the input stripped of all whitespace contains the // expected markup. assert(actual.includes(expected), `ACTUAL: ${actual}\nEXPECTED: ${expected}`); });