* This handles links after the code is rendered so that * we can link method types and interfaces directly from the code.
()
| 13 | * we can link method types and interfaces directly from the code. |
| 14 | */ |
| 15 | function transformCustomLinks() { |
| 16 | function getLinkNode(text) { |
| 17 | const linkMatch = text.match(/<a href="(\/\S+)">([^\s<]+)<\/a>/); |
| 18 | |
| 19 | if (!linkMatch) { |
| 20 | return null; |
| 21 | } |
| 22 | |
| 23 | const [, linkHref, linkText] = linkMatch; |
| 24 | return { |
| 25 | type: "element", |
| 26 | tagName: "a", |
| 27 | properties: { href: linkHref }, |
| 28 | children: [{ type: "text", value: linkText }], |
| 29 | }; |
| 30 | } |
| 31 | |
| 32 | return (tree) => { |
| 33 | visit(tree, "element", (code) => { |
| 34 | if (code.tagName !== "code") { |
| 35 | return CONTINUE; |
| 36 | } |
| 37 | |
| 38 | let start = null; |
| 39 | visit(code, "element", (node, index, parent) => { |
| 40 | if ( |
| 41 | node.children.length !== 1 || |
| 42 | node.children[0].type !== "text" |
| 43 | ) { |
| 44 | return CONTINUE; |
| 45 | } |
| 46 | |
| 47 | const text = node.children[0].value.trim(); |
| 48 | |
| 49 | if (text === "`<") { |
| 50 | start = index; |
| 51 | return CONTINUE; |
| 52 | } |
| 53 | |
| 54 | if (text === ">`") { |
| 55 | const text = parent.children |
| 56 | .slice(start, index + 1) |
| 57 | .map((e) => e.children[0].value); |
| 58 | const linkNode = getLinkNode(text.join("")); |
| 59 | |
| 60 | if (linkNode) { |
| 61 | linkNode.children[0].value = |
| 62 | " " + linkNode.children[0].value; |
| 63 | parent.children.splice( |
| 64 | start, |
| 65 | index + 1 - start, |
| 66 | linkNode |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | start = null; |
| 71 | return CONTINUE; |
| 72 | } |
nothing calls this directly
no test coverage detected