| 1 | import type { Nodes } from "mdast"; |
| 2 | import { fromMarkdown } |
| 3 | import { gfmFromMarkdown } |
| 4 | import { gfm } |
| 5 | import { |
| 6 | anyOf, |
| 7 | buildRegExp, |
| 8 | capture, |
| 9 | charClass, |
| 10 | choiceOf, |
| 11 | digit, |
| 12 | negativeLookahead, |
| 13 | negativeLookbehind, |
| 14 | oneOrMore, |
| 15 | optional, |
| 16 | whitespace, |
| 17 | word, |
| 18 | zeroOrMore, |
| 19 | } from "ts-regex-builder"; |
| 20 | |
| 21 | // Usernames and slugs may contain dots, but never end in one, so `@bob.` at the end of a sentence stays `bob`. |
| 22 | const name = ( |
| 23 | word, |
| 24 | optional([zeroOrMore(charClass |
| 25 | ]; |
| 26 | |
| 27 | // `abc#1`, `{` and `a/#1` are not references; neither is `@1` glued to a word, like an email address. |
| 28 | const standalone = negativeLookbehind |
| 29 | const end = negativeLookahead |
| 30 | |
| 31 | // `issue.number` is a Postgres `integer`; a bigger number cannot name an issue and would fail the lookup. |
| 32 | const MAX_ISSUE_NUMBER = 2_147_483_647 |
| 33 | |
| 34 | /** Each entry is one kind of reference. Add one here and `Reference` grows a matching variant; `read` returning null drops the match. */ |
| 35 | const definitions = { |
| 36 | issue: { |
| 37 | pattern: buildRegExp( |
| 38 | [ |
| 39 | standalone, |
| 40 | optional([ |
| 41 | capture( |
| 42 | choiceOf( |
| 43 | "closes", |
| 44 | "closed", |
| 45 | "close", |
| 46 | "fixes", |
| 47 | "fixed", |
| 48 | "fix", |
| 49 | "resolves", |
| 50 | "resolved", |
| 51 | "resolve", |
| 52 | ), |
| 53 | { name: "keyword" |
| 54 | ), |
| 55 | optional(":"), |
| 56 | oneOrMore(whitespace), |
| 57 | ]), |
| 58 | capture( |
| 59 | [ |
| 60 | optional([ |
| 61 | capture(name(".- |
| 62 | "/", |
| 63 | capture(name(".- |
| 64 | ]), |
| 65 | "#", |
| 66 | capture(oneOrMore(digit |
| 67 | ], |
| 68 | { name: "target" |
| 69 | ), |
| 70 | end, |
| 71 | ], |
| 72 | { global: true, ignoreCase |
| 73 | ), |
| 74 | read: (groups: |
| 75 | const number = |
| 76 | if (number < |
| 77 | return { |
| 78 | owner: groups.owner |
| 79 | repo: groups.repo |
| 80 | number, |
| 81 | closing: groups.keyword |
| 82 | }; |
| 83 | }, |
| 84 | }, |
| 85 | mention: { |
| 86 | pattern: buildRegExp( |
| 87 | [ |
| 88 | standalone, |
| 89 | capture(["@", |
| 90 | name: "target", |
| 91 | }), |
| 92 | end, |
| 93 | ], |
| 94 | { global: true, hasIndices |
| 95 | ), |
| 96 | read: (groups: |
| 97 | username: groups.username |
| 98 | }), |
| 99 | }, |
| 100 | }; |
| 101 | |
| 102 | type Definitions |
| 103 | |
| 104 | export type Reference |
| 105 | [Kind |
| 106 | kind: Kind |
| 107 | /** Offset of the linkable part (`#1`, not `closes #1`) in the text it was found in. */ |
| 108 | index: number |
| 109 | length: number |
| 110 | } & NonNullable |
| 111 | }[keyof Definitions |
| 112 | |
| 113 | /** References in plain text, sorted by position. The text must already be free of code; `parseReferences` does that for markdown. */ |
| 114 | export function findReferences(text |
| 115 | const found: |
| 116 | for (const [kind |
| 117 | for (const match |
| 118 | const [index, |
| 119 | match.index, |
| 120 | match.index + |
| 121 | ]; |
| 122 | const fields = |
| 123 | if (fields) { |
| 124 | found.push({ |
| 125 | kind, |
| 126 | index, |
| 127 | length: stop - |
| 128 | ...fields, |
| 129 | } as Reference) |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | return found.sort |
| 134 | } |
| 135 | |
| 136 | /** References in a markdown document, ignoring code and link text exactly as the renderer does. */ |
| 137 | export function parseReferences(markdown |
| 138 | const tree = |
| 139 | extensions: [gfm()], |
| 140 | mdastExtensions: [gfmFromMarkdown()], |
| 141 | }); |
| 142 | const found: |
| 143 | const walk = |
| 144 | if (node.type |
| 145 | else if ( |
| 146 | "children" in node |
| 147 | node.type !== |
| 148 | node.type !== |
| 149 | ) { |
| 150 | for (const child |
| 151 | } |
| 152 | }; |
| 153 | walk(tree); |
| 154 | return found; |
| 155 | } |