Skip to content

Commit

Permalink
tools: update doc generator dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Sep 9, 2021
1 parent 513a979 commit 130a233
Show file tree
Hide file tree
Showing 10 changed files with 1,619 additions and 1,039 deletions.
23 changes: 11 additions & 12 deletions test/doctool/test-doctool-html.mjs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -396,7 +396,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(headingText, idCounters);
Expand Down
8 changes: 4 additions & 4 deletions tools/doc/json.mjs
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 130a233

Please sign in to comment.