9
9
parseHeadingIntoMetadata ,
10
10
parseYAMLIntoMetadata ,
11
11
transformTypeToReferenceLink ,
12
+ transformUnixManualToLink ,
12
13
} from '../parser/index.mjs' ;
13
14
import { getRemark } from '../remark.mjs' ;
14
15
import { transformNodesToString } from '../unist.mjs' ;
@@ -64,18 +65,17 @@ const createQueries = () => {
64
65
} ;
65
66
66
67
/**
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
69
69
*
70
- * @param {import('@types/mdast').Text } node A Markdown link node
70
+ * @param {import('@types/mdast').Text } node The current node
71
71
* @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
+ *
72
75
*/
73
- const updateTypeReference = ( node , parent ) => {
76
+ const updateReferences = ( query , transformer , node , parent ) => {
74
77
const replacedTypes = node . value
75
- . replace (
76
- createQueries . QUERIES . normalizeTypes ,
77
- transformTypeToReferenceLink
78
- )
78
+ . replace ( query , transformer )
79
79
// Remark doesn't handle leading / trailing spaces, so replace them with
80
80
// HTML entities.
81
81
. replace ( / ^ \s / , ' ' )
@@ -172,7 +172,20 @@ const createQueries = () => {
172
172
addYAMLMetadata,
173
173
setHeadingMetadata,
174
174
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
+ ) ,
176
189
updateLinkReference,
177
190
addStabilityMetadata,
178
191
updateStabilityPrefixToLink,
@@ -195,6 +208,8 @@ createQueries.QUERIES = {
195
208
stabilityIndexPrefix : / S t a b i l i t y : ( [ 0 - 5 ] ) / ,
196
209
// ReGeX for retrieving the inner content from a YAML block
197
210
yamlInnerContent : / ^ < ! - - [ ] ? (?: Y A M L ( [ \s \S ] * ?) | ( [ \S ] * ?) ) ? [ ] ? - - > / ,
211
+ // RegEX for finding references to Unix manuals
212
+ unixManualPage : / \b ( [ a - z . ] + ) \( ( \d ) ( [ a - z ] ? ) \) / gm,
198
213
} ;
199
214
200
215
createQueries . UNIST = {
@@ -217,6 +232,12 @@ createQueries.UNIST = {
217
232
*/
218
233
isTextWithType : ( { type, value } ) =>
219
234
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 ) ,
220
241
/**
221
242
* @param {import('@types/mdast').Html } html
222
243
* @returns {boolean }
0 commit comments