11cb0ef41Sopenharmony_ciimport '../common/index.mjs';
21cb0ef41Sopenharmony_ciimport * as fixtures from '../common/fixtures.mjs';
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciimport assert from 'assert';
51cb0ef41Sopenharmony_ciimport { readFileSync } from 'fs';
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciimport * as html from '../../tools/doc/html.mjs';
81cb0ef41Sopenharmony_ciimport { replaceLinks } from '../../tools/doc/markdown.mjs';
91cb0ef41Sopenharmony_ciimport {
101cb0ef41Sopenharmony_ci  rehypeRaw,
111cb0ef41Sopenharmony_ci  rehypeStringify,
121cb0ef41Sopenharmony_ci  remarkParse,
131cb0ef41Sopenharmony_ci  remarkRehype,
141cb0ef41Sopenharmony_ci  unified,
151cb0ef41Sopenharmony_ci} from '../../tools/doc/deps.mjs';
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci// Test links mapper is an object of the following structure:
181cb0ef41Sopenharmony_ci// {
191cb0ef41Sopenharmony_ci//   [filename]: {
201cb0ef41Sopenharmony_ci//     [link definition identifier]: [url to the linked resource]
211cb0ef41Sopenharmony_ci//   }
221cb0ef41Sopenharmony_ci// }
231cb0ef41Sopenharmony_ciconst testLinksMapper = {
241cb0ef41Sopenharmony_ci  'foo': {
251cb0ef41Sopenharmony_ci    'command line options': 'cli.html#cli-options',
261cb0ef41Sopenharmony_ci    'web server': 'example.html',
271cb0ef41Sopenharmony_ci  },
281cb0ef41Sopenharmony_ci};
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction toHTML({ input, filename, nodeVersion, versions }) {
311cb0ef41Sopenharmony_ci  const content = unified()
321cb0ef41Sopenharmony_ci    .use(replaceLinks, { filename, linksMapper: testLinksMapper })
331cb0ef41Sopenharmony_ci    .use(remarkParse)
341cb0ef41Sopenharmony_ci    .use(html.firstHeader)
351cb0ef41Sopenharmony_ci    .use(html.preprocessText, { nodeVersion })
361cb0ef41Sopenharmony_ci    .use(html.preprocessElements, { filename })
371cb0ef41Sopenharmony_ci    .use(html.buildToc, { filename, apilinks: {} })
381cb0ef41Sopenharmony_ci    .use(remarkRehype, { allowDangerousHtml: true })
391cb0ef41Sopenharmony_ci    .use(rehypeRaw)
401cb0ef41Sopenharmony_ci    .use(rehypeStringify)
411cb0ef41Sopenharmony_ci    .processSync(input);
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci  return html.toHTML({ input, content, filename, nodeVersion, versions });
441cb0ef41Sopenharmony_ci}
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci// Test data is a list of objects with two properties.
471cb0ef41Sopenharmony_ci// The file property is the file path.
481cb0ef41Sopenharmony_ci// The html property is some HTML which will be generated by the doctool.
491cb0ef41Sopenharmony_ci// This HTML will be stripped of all whitespace because we don't currently
501cb0ef41Sopenharmony_ci// have an HTML parser.
511cb0ef41Sopenharmony_ciconst testData = [
521cb0ef41Sopenharmony_ci  {
531cb0ef41Sopenharmony_ci    file: fixtures.path('order_of_end_tags_5873.md'),
541cb0ef41Sopenharmony_ci    html: '<h4>Static method: Buffer.from(array) <span> ' +
551cb0ef41Sopenharmony_ci      '<a class="mark" href="#static-method-bufferfromarray" ' +
561cb0ef41Sopenharmony_ci      'id="static-method-bufferfromarray">#</a> </span> ' +
571cb0ef41Sopenharmony_ci      '<a aria-hidden="true" class="legacy" ' +
581cb0ef41Sopenharmony_ci      'id="foo_static_method_buffer_from_array"></a></h4>' +
591cb0ef41Sopenharmony_ci      '<ul><li><code>array</code><a ' +
601cb0ef41Sopenharmony_ci      'href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/' +
611cb0ef41Sopenharmony_ci      'Reference/Global_Objects/Array" class="type">&#x3C;Array></a></li></ul>',
621cb0ef41Sopenharmony_ci  },
631cb0ef41Sopenharmony_ci  {
641cb0ef41Sopenharmony_ci    file: fixtures.path('doc_with_yaml.md'),
651cb0ef41Sopenharmony_ci    html: '<h2>Sample Markdown with YAML info' +
661cb0ef41Sopenharmony_ci      '<span><a class="mark" href="#sample-markdown-with-yaml-info" ' +
671cb0ef41Sopenharmony_ci      ' id="sample-markdown-with-yaml-info">#</a></span>' +
681cb0ef41Sopenharmony_ci      '<a aria-hidden="true" class="legacy" ' +
691cb0ef41Sopenharmony_ci      'id="foo_sample_markdown_with_yaml_info"></a></h2>' +
701cb0ef41Sopenharmony_ci      '<section><h3>Foobar<span><a class="mark" href="#foobar" ' +
711cb0ef41Sopenharmony_ci      'id="foobar">#</a></span>' +
721cb0ef41Sopenharmony_ci      '<a aria-hidden="true" class="legacy" id="foo_foobar"></a></h3>' +
731cb0ef41Sopenharmony_ci      '<div class="api_metadata"><span>Added in: v1.0.0</span></div> ' +
741cb0ef41Sopenharmony_ci      '<p>Describe <code>Foobar</code> in more detail here.</p>' +
751cb0ef41Sopenharmony_ci      '</section><section>' +
761cb0ef41Sopenharmony_ci      '<h3>Foobar II<span><a class="mark" href="#foobar-ii" ' +
771cb0ef41Sopenharmony_ci      'id="foobar-ii">#</a></span>' +
781cb0ef41Sopenharmony_ci      '<a aria-hidden="true" class="legacy" id="foo_foobar_ii"></a></h3>' +
791cb0ef41Sopenharmony_ci      '<div class="api_metadata">' +
801cb0ef41Sopenharmony_ci      '<details class="changelog"><summary>History</summary>' +
811cb0ef41Sopenharmony_ci      '<table><tbody><tr><th>Version</th><th>Changes</th></tr>' +
821cb0ef41Sopenharmony_ci      '<tr><td>v4.2.0</td><td><p>The <code>error</code> parameter can now be' +
831cb0ef41Sopenharmony_ci      'an arrow function.</p></td></tr>' +
841cb0ef41Sopenharmony_ci      '<tr><td>v5.3.0, v4.2.0</td>' +
851cb0ef41Sopenharmony_ci      '<td><p><span>Added in: v5.3.0, v4.2.0</span></p></td></tr>' +
861cb0ef41Sopenharmony_ci      '</tbody></table></details></div> ' +
871cb0ef41Sopenharmony_ci      '<p>Describe <code>Foobar II</code> in more detail here.' +
881cb0ef41Sopenharmony_ci      '<a href="http://man7.org/linux/man-pages/man1/fg.1.html"><code>fg(1)' +
891cb0ef41Sopenharmony_ci      '</code></a></p></section><section>' +
901cb0ef41Sopenharmony_ci      '<h3>Deprecated thingy<span><a class="mark" ' +
911cb0ef41Sopenharmony_ci      'href="#deprecated-thingy" id="deprecated-thingy">#</a>' +
921cb0ef41Sopenharmony_ci      '</span><a aria-hidden="true" class="legacy"' +
931cb0ef41Sopenharmony_ci      'id="foo_deprecated_thingy"></a>' +
941cb0ef41Sopenharmony_ci      '</h3><div class="api_metadata"><span>Added in: v1.0.0</span>' +
951cb0ef41Sopenharmony_ci      '<span>Deprecated since: v2.0.0</span></div><p>Describe ' +
961cb0ef41Sopenharmony_ci      '<code>Deprecated thingy</code> in more detail here.' +
971cb0ef41Sopenharmony_ci      '<a href="http://man7.org/linux/man-pages/man1/fg.1p.html"><code>fg(1p)' +
981cb0ef41Sopenharmony_ci      '</code></a></p></section><section>' +
991cb0ef41Sopenharmony_ci      '<h3>Something<span><a class="mark" href="#something' +
1001cb0ef41Sopenharmony_ci      '" id="something">#</a></span>' +
1011cb0ef41Sopenharmony_ci      '<a aria-hidden="true" class="legacy" id="foo_something"></a></h3>' +
1021cb0ef41Sopenharmony_ci      '<!-- This is not a metadata comment --> ' +
1031cb0ef41Sopenharmony_ci      '<p>Describe <code>Something</code> in more detail here. </p></section>',
1041cb0ef41Sopenharmony_ci  },
1051cb0ef41Sopenharmony_ci  {
1061cb0ef41Sopenharmony_ci    file: fixtures.path('sample_document.md'),
1071cb0ef41Sopenharmony_ci    html: '<ol><li>fish</li><li>fish</li></ol>' +
1081cb0ef41Sopenharmony_ci      '<ul><li>Red fish</li><li>Blue fish</li></ul>',
1091cb0ef41Sopenharmony_ci  },
1101cb0ef41Sopenharmony_ci  {
1111cb0ef41Sopenharmony_ci    file: fixtures.path('altdocs.md'),
1121cb0ef41Sopenharmony_ci    html: '<li><a href="https://nodejs.org/docs/latest-v8.x/api/foo.html">8.x',
1131cb0ef41Sopenharmony_ci  },
1141cb0ef41Sopenharmony_ci  {
1151cb0ef41Sopenharmony_ci    file: fixtures.path('document_with_links.md'),
1161cb0ef41Sopenharmony_ci    html: '<h2>Usage and Example<span><a class="mark"' +
1171cb0ef41Sopenharmony_ci    'href="#usage-and-example" id="usage-and-example">#</a>' +
1181cb0ef41Sopenharmony_ci    '</span><a aria-hidden="true" class="legacy" id="foo_usage_and_example">' +
1191cb0ef41Sopenharmony_ci    '</a></h2><section><h3>Usage<span><a class="mark" href="#usage"' +
1201cb0ef41Sopenharmony_ci    'id="usage">#</a></span><a aria-hidden="true" class="legacy"' +
1211cb0ef41Sopenharmony_ci    'id="foo_usage"></a></h3><p><code>node \\[options\\] index.js' +
1221cb0ef41Sopenharmony_ci    '</code></p><p>Please see the<a href="cli.html#cli-options">' +
1231cb0ef41Sopenharmony_ci    'Command Line Options</a>document for more information.</p>' +
1241cb0ef41Sopenharmony_ci    '</section><section><h3>' +
1251cb0ef41Sopenharmony_ci    'Example<span><a class="mark" href="#example" id="example">' +
1261cb0ef41Sopenharmony_ci    '#</a></span><a aria-hidden="true" class="legacy" id="foo_example">' +
1271cb0ef41Sopenharmony_ci    '</a></h3><p>An example of a<a href="example.html">' +
1281cb0ef41Sopenharmony_ci    'webserver</a>written with Node.js which responds with<code>' +
1291cb0ef41Sopenharmony_ci    '\'Hello, World!\'</code>:</p></section><section>' +
1301cb0ef41Sopenharmony_ci    '<h3>See also<span><a class="mark"' +
1311cb0ef41Sopenharmony_ci    'href="#see-also" id="see-also">#</a></span><a aria-hidden="true"' +
1321cb0ef41Sopenharmony_ci    'class="legacy" id="foo_see_also"></a></h3><p>Check' +
1331cb0ef41Sopenharmony_ci    'out also<a href="https://nodejs.org/">this guide</a></p></section>',
1341cb0ef41Sopenharmony_ci  },
1351cb0ef41Sopenharmony_ci  {
1361cb0ef41Sopenharmony_ci    file: fixtures.path('document_with_special_heading.md'),
1371cb0ef41Sopenharmony_ci    html: '<title>Sample markdown with special heading |',
1381cb0ef41Sopenharmony_ci  },
1391cb0ef41Sopenharmony_ci  {
1401cb0ef41Sopenharmony_ci    file: fixtures.path('document_with_esm_and_cjs_code_snippet.md'),
1411cb0ef41Sopenharmony_ci    html: '<input class="js-flavor-selector" type="checkbox" checked',
1421cb0ef41Sopenharmony_ci  },
1431cb0ef41Sopenharmony_ci  {
1441cb0ef41Sopenharmony_ci    file: fixtures.path('document_with_cjs_and_esm_code_snippet.md'),
1451cb0ef41Sopenharmony_ci    html: '<input class="js-flavor-selector" type="checkbox" aria-label',
1461cb0ef41Sopenharmony_ci  },
1471cb0ef41Sopenharmony_ci];
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciconst spaces = /\s/g;
1501cb0ef41Sopenharmony_ciconst versions = [
1511cb0ef41Sopenharmony_ci  { num: '10.x', lts: true },
1521cb0ef41Sopenharmony_ci  { num: '9.x' },
1531cb0ef41Sopenharmony_ci  { num: '8.x' },
1541cb0ef41Sopenharmony_ci  { num: '7.x' },
1551cb0ef41Sopenharmony_ci  { num: '6.x' },
1561cb0ef41Sopenharmony_ci  { num: '5.x' },
1571cb0ef41Sopenharmony_ci  { num: '4.x' },
1581cb0ef41Sopenharmony_ci  { num: '0.12.x' },
1591cb0ef41Sopenharmony_ci  { num: '0.10.x' }];
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_citestData.forEach(({ file, html }) => {
1621cb0ef41Sopenharmony_ci  // Normalize expected data by stripping whitespace.
1631cb0ef41Sopenharmony_ci  const expected = html.replace(spaces, '');
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci  const input = readFileSync(file, 'utf8');
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ci  const output = toHTML({ input,
1681cb0ef41Sopenharmony_ci                          filename: 'foo',
1691cb0ef41Sopenharmony_ci                          nodeVersion: process.version,
1701cb0ef41Sopenharmony_ci                          versions });
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci  const actual = output.replace(spaces, '');
1731cb0ef41Sopenharmony_ci  // Assert that the input stripped of all whitespace contains the
1741cb0ef41Sopenharmony_ci  // expected markup.
1751cb0ef41Sopenharmony_ci  assert(actual.includes(expected),
1761cb0ef41Sopenharmony_ci         `ACTUAL: ${actual}\nEXPECTED: ${expected}`);
1771cb0ef41Sopenharmony_ci});
178