Skip to content

Commit c6eb43b

Browse files
committed
fix(parser): transform man pages
1 parent f80bcce commit c6eb43b

File tree

4 files changed

+57
-9
lines changed

4 files changed

+57
-9
lines changed

src/generators/metadata/utils/parse.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const parseApiDoc = ({ file, tree }) => {
3535
addYAMLMetadata,
3636
updateMarkdownLink,
3737
updateTypeReference,
38+
updateUnixManualReference,
3839
updateLinkReference,
3940
addStabilityMetadata,
4041
} = createQueries();
@@ -127,6 +128,13 @@ export const parseApiDoc = ({ file, tree }) => {
127128
updateTypeReference(node, parent)
128129
);
129130

131+
// Visits all Unix manual references, and replaces them with links
132+
visit(
133+
subTree,
134+
createQueries.UNIST.isTextWithUnixManual,
135+
(node, _, parent) => updateUnixManualReference(node, parent)
136+
);
137+
130138
// Removes already parsed items from the subtree so that they aren't included in the final content
131139
remove(subTree, [createQueries.UNIST.isYamlNode]);
132140

src/utils/parser/constants.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
// This is the base URL of the MDN Web documentation
44
export const DOC_MDN_BASE_URL = 'https://developer.mozilla.org/en-US/docs/Web/';
55

6+
// The is the base URL of the Man7 documentation
7+
export const DOC_MAN_BASE_URL = 'http://man7.org/linux/man-pages/man';
8+
69
// This is the base URL for the MDN JavaScript documentation
710
export const DOC_MDN_BASE_URL_JS = `${DOC_MDN_BASE_URL}JavaScript/`;
811

src/utils/parser/index.mjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
DOC_TYPES_MAPPING_NODE_MODULES,
1111
DOC_TYPES_MAPPING_OTHER,
1212
DOC_TYPES_MAPPING_PRIMITIVES,
13+
DOC_MAN_BASE_URL,
1314
} from './constants.mjs';
1415
import createQueries from '../queries/index.mjs';
1516

@@ -44,6 +45,21 @@ export const normalizeYamlSyntax = yamlContent => {
4445
.replace(/^[\r\n]+|[\r\n]+$/g, ''); // Remove initial and final line breaks
4546
};
4647

48+
/**
49+
* @param {string} text The inner text
50+
* @param {string} command The manual page
51+
* @param {string} sectionNumber The manual section
52+
* @param {string} sectionLetter The manual section number
53+
*/
54+
export const transformUnixManualToLink = (
55+
text,
56+
command,
57+
sectionNumber,
58+
sectionLetter
59+
) => {
60+
return `[\`${text}\`](${DOC_MAN_BASE_URL}${sectionNumber}/${command}.${sectionNumber}${sectionLetter}.html)`;
61+
};
62+
4763
/**
4864
* This method replaces plain text Types within the Markdown content into Markdown links
4965
* that link to the actual relevant reference for such type (either internal or external link)

src/utils/queries/index.mjs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
parseHeadingIntoMetadata,
1010
parseYAMLIntoMetadata,
1111
transformTypeToReferenceLink,
12+
transformUnixManualToLink,
1213
} from '../parser/index.mjs';
1314
import { getRemark } from '../remark.mjs';
1415
import { transformNodesToString } from '../unist.mjs';
@@ -64,18 +65,17 @@ const createQueries = () => {
6465
};
6566

6667
/**
67-
* Updates a Markdown text containing an API type reference
68-
* into a Markdown link referencing to the correct API docs
68+
* Updates a reference
6969
*
70-
* @param {import('@types/mdast').Text} node A Markdown link node
70+
* @param {import('@types/mdast').Text} node The current node
7171
* @param {import('@types/mdast').Parent} parent The parent node
72+
* @param {string|RegExp} query The search query
73+
* @param {Function} transformer The function to transform the reference
74+
*
7275
*/
73-
const updateTypeReference = (node, parent) => {
76+
const updateReferences = (query, transformer, node, parent) => {
7477
const replacedTypes = node.value
75-
.replace(
76-
createQueries.QUERIES.normalizeTypes,
77-
transformTypeToReferenceLink
78-
)
78+
.replace(query, transformer)
7979
// Remark doesn't handle leading / trailing spaces, so replace them with
8080
// HTML entities.
8181
.replace(/^\s/, ' ')
@@ -172,7 +172,20 @@ const createQueries = () => {
172172
addYAMLMetadata,
173173
setHeadingMetadata,
174174
updateMarkdownLink,
175-
updateTypeReference,
175+
/** @param {Array<import('@types/mdast').Node>} args */
176+
updateTypeReference: (...args) =>
177+
updateReferences(
178+
createQueries.QUERIES.normalizeTypes,
179+
transformTypeToReferenceLink,
180+
...args
181+
),
182+
/** @param {Array<import('@types/mdast').Node>} args */
183+
updateUnixManualReference: (...args) =>
184+
updateReferences(
185+
createQueries.QUERIES.unixManualPage,
186+
transformUnixManualToLink,
187+
...args
188+
),
176189
updateLinkReference,
177190
addStabilityMetadata,
178191
updateStabilityPrefixToLink,
@@ -195,6 +208,8 @@ createQueries.QUERIES = {
195208
stabilityIndexPrefix: /Stability: ([0-5])/,
196209
// ReGeX for retrieving the inner content from a YAML block
197210
yamlInnerContent: /^<!--[ ]?(?:YAML([\s\S]*?)|([ \S]*?))?[ ]?-->/,
211+
// RegEX for finding references to Unix manuals
212+
unixManualPage: /\b([a-z.]+)\((\d)([a-z]?)\)/gm,
198213
};
199214

200215
createQueries.UNIST = {
@@ -217,6 +232,12 @@ createQueries.UNIST = {
217232
*/
218233
isTextWithType: ({ type, value }) =>
219234
type === 'text' && createQueries.QUERIES.normalizeTypes.test(value),
235+
/**
236+
* @param {import('@types/mdast').Text} text
237+
* @returns {boolean}
238+
*/
239+
isTextWithUnixManual: ({ type, value }) =>
240+
type === 'text' && createQueries.QUERIES.unixManualPage.test(value),
220241
/**
221242
* @param {import('@types/mdast').Html} html
222243
* @returns {boolean}

0 commit comments

Comments
 (0)