1 | /**
|
2 | * Create a tokenizer method for Remark, our Markdown processor,
|
3 | * that is able to parse JSDoc inline tokens
|
4 | *
|
5 | * @private
|
6 | * @param {string} type the destination type of the parsed objects
|
7 | * @param {RegExp} regex regular expression for extracting content
|
8 | * from text
|
9 | * @returns {Function} tokenizer
|
10 | */
|
11 | function makeTokenizer(type, regex) {
|
12 | const tokenizer = function(eat, value) {
|
13 | const match = regex.exec(value);
|
14 |
|
15 | if (!match) {
|
16 | return;
|
17 | }
|
18 |
|
19 | return eat(match[0])({
|
20 | type,
|
21 | url: match[1],
|
22 | title: null,
|
23 | jsdoc: true,
|
24 | children: [
|
25 | {
|
26 | type: 'text',
|
27 | value: match[2] || match[1]
|
28 | }
|
29 | ]
|
30 | });
|
31 | };
|
32 |
|
33 | tokenizer.notInLink = true;
|
34 | tokenizer.locator = function(value, fromIndex) {
|
35 | return value.indexOf('{@' + type, fromIndex);
|
36 | };
|
37 |
|
38 | return tokenizer;
|
39 | }
|
40 |
|
41 | const tokenizeLink = makeTokenizer('link', /^\{@link\s+(.+?)(?:[\s|](.*?))?\}/);
|
42 | const tokenizeTutorial = makeTokenizer(
|
43 | 'tutorial',
|
44 | /^\{@tutorial\s+(.+?)(?:[\s|](.*?))?\}/
|
45 | );
|
46 |
|
47 | /**
|
48 | * A remark plugin that installs
|
49 | * [tokenizers](https://github.com/wooorm/remark/blob/master/doc/remarkplugin.3.md#function-tokenizereat-value-silent)
|
50 | * and [locators](https://github.com/wooorm/remark/blob/master/doc/remarkplugin.3.md#function-locatorvalue-fromindex)
|
51 | * for JSDoc inline `{@link}` and `{@tutorial}` tags.
|
52 | *
|
53 | * This does not handle the `[text]({@link url})` and `[text]({@tutorial url})` forms of these tags.
|
54 | * That's a JSDoc misfeature; just use regular markdown syntax instead: `[text](url)`.
|
55 | *
|
56 | * @returns {undefined}
|
57 | */
|
58 | module.exports = function(/* options: Object*/) {
|
59 | const proto = this.Parser.prototype;
|
60 | proto.inlineTokenizers.tokenizeLink = tokenizeLink;
|
61 | proto.inlineTokenizers.tokenizeTutorial = tokenizeTutorial;
|
62 | const methods = proto.inlineMethods;
|
63 | methods.splice(
|
64 | methods.indexOf('inlineText'),
|
65 | 0,
|
66 | 'tokenizeLink',
|
67 | 'tokenizeTutorial'
|
68 | );
|
69 | };
|