Skip to content

Commit

Permalink
tools: update doc generator dependencies
Browse files Browse the repository at this point in the history
PR-URL: #40042
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
targos authored and BethGriggs committed Sep 21, 2021
1 parent 98d42fa commit a8c99d9
Show file tree
Hide file tree
Showing 10 changed files with 1,612 additions and 1,060 deletions.
23 changes: 11 additions & 12 deletions test/doctool/test-doctool-html.mjs
Expand Up @@ -3,17 +3,16 @@ import * as fixtures from '../common/fixtures.mjs';

import assert from 'assert';
import { readFileSync } from 'fs';
import { createRequire } from 'module';

import * as html from '../../tools/doc/html.mjs';
import { replaceLinks } from '../../tools/doc/markdown.mjs';

const require = createRequire(new URL('../../tools/doc/', import.meta.url));
const unified = require('unified');
const markdown = require('remark-parse');
const remark2rehype = require('remark-rehype');
const raw = require('rehype-raw');
const htmlStringify = require('rehype-stringify');
import {
rehypeRaw,
rehypeStringify,
remarkParse,
remarkRehype,
unified,
} from '../../tools/doc/deps.mjs';

// Test links mapper is an object of the following structure:
// {
Expand All @@ -31,14 +30,14 @@ const testLinksMapper = {
function toHTML({ input, filename, nodeVersion, versions }) {
const content = unified()
.use(replaceLinks, { filename, linksMapper: testLinksMapper })
.use(markdown)
.use(remarkParse)
.use(html.firstHeader)
.use(html.preprocessText, { nodeVersion })
.use(html.preprocessElements, { filename })
.use(html.buildToc, { filename, apilinks: {} })
.use(remark2rehype, { allowDangerousHtml: true })
.use(raw)
.use(htmlStringify)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeRaw)
.use(rehypeStringify)
.processSync(input);

return html.toHTML({ input, content, filename, nodeVersion, versions });
Expand Down
11 changes: 5 additions & 6 deletions test/doctool/test-doctool-json.mjs
Expand Up @@ -3,21 +3,20 @@ import * as fixtures from '../common/fixtures.mjs';

import assert from 'assert';
import fs from 'fs';
import { createRequire } from 'module';

import * as json from '../../tools/doc/json.mjs';

const require = createRequire(new URL('../../tools/doc/', import.meta.url));
const unified = require('unified');
const markdown = require('remark-parse');
import {
remarkParse,
unified,
} from '../../tools/doc/deps.mjs';

function toJSON(input, filename, cb) {
function nullCompiler() {
this.Compiler = (tree) => tree;
}

unified()
.use(markdown)
.use(remarkParse)
.use(json.jsonAPI, { filename })
.use(nullCompiler)
.process(input, cb);
Expand Down
2 changes: 1 addition & 1 deletion tools/doc/addon-verify.mjs
Expand Up @@ -9,7 +9,7 @@ import { mkdir, writeFile } from 'fs/promises';
import gfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import { toVFile } from 'to-vfile';
import unified from 'unified';
import { unified } from 'unified';

const rootDir = new URL('../../', import.meta.url);
const doc = new URL('./doc/api/addons.md', rootDir);
Expand Down
7 changes: 7 additions & 0 deletions tools/doc/deps.mjs
@@ -0,0 +1,7 @@
// Re-exporting dependencies for tests in test/doctool.

export { default as rehypeRaw } from 'rehype-raw';
export { default as rehypeStringify } from 'rehype-stringify';
export { default as remarkParse } from 'remark-parse';
export { default as remarkRehype } from 'remark-rehype';
export { unified } from 'unified';
2 changes: 1 addition & 1 deletion tools/doc/generate.mjs
Expand Up @@ -28,7 +28,7 @@ import gfm from 'remark-gfm';
import markdown from 'remark-parse';
import remark2rehype from 'remark-rehype';
import frontmatter from 'remark-frontmatter';
import unified from 'unified';
import { unified } from 'unified';

import * as html from './html.mjs';
import * as json from './json.mjs';
Expand Down
4 changes: 2 additions & 2 deletions tools/doc/html.mjs
Expand Up @@ -28,7 +28,7 @@ import htmlStringify from 'rehype-stringify';
import gfm from 'remark-gfm';
import markdown from 'remark-parse';
import remark2rehype from 'remark-rehype';
import unified from 'unified';
import { unified } from 'unified';
import { visit } from 'unist-util-visit';

import * as common from './common.mjs';
Expand Down Expand Up @@ -395,7 +395,7 @@ export function buildToc({ filename, apilinks }) {

depth = node.depth;
const realFilename = path.basename(filename, '.md');
const headingText = file.contents.slice(
const headingText = file.value.slice(
node.children[0].position.start.offset,
node.position.end.offset).trim();
const id = getId(`${realFilename}_${headingText}`, idCounters);
Expand Down
8 changes: 4 additions & 4 deletions tools/doc/json.mjs
Expand Up @@ -20,7 +20,7 @@
// USE OR OTHER DEALINGS IN THE SOFTWARE.

import html from 'remark-html';
import unified from 'unified';
import { unified } from 'unified';
import { selectAll } from 'unist-util-select';

import * as common from './common.mjs';
Expand Down Expand Up @@ -376,7 +376,7 @@ function parseListItem(item, file) {

current.textRaw = item.children.filter((node) => node.type !== 'list')
.map((node) => (
file.contents.slice(node.position.start.offset, node.position.end.offset))
file.value.slice(node.position.start.offset, node.position.end.offset))
)
.join('').replace(/\s+/g, ' ').replace(/<!--.*?-->/sg, '');
let text = current.textRaw;
Expand Down Expand Up @@ -491,8 +491,8 @@ function newSection(header, file) {
function textJoin(nodes, file) {
return nodes.map((node) => {
if (node.type === 'linkReference') {
return file.contents.slice(node.position.start.offset,
node.position.end.offset);
return file.value.slice(node.position.start.offset,
node.position.end.offset);
} else if (node.type === 'inlineCode') {
return `\`${node.value}\``;
} else if (node.type === 'strong') {
Expand Down

0 comments on commit a8c99d9

Please sign in to comment.