blob: e41dc51457a45a000488eab8e0ee87b70035e567 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import { buildTextToken, buildUneditableInlineTokens } from './build_uneditable_token';
/*
Use case examples:
- Majority: two bracket pairs, back-to-back, each with content (including spaces)
- `[environment terraform plans][terraform]`
- `[an issue labelled `~"main:broken"`][broken-main-issues]`
- Minority: two bracket pairs the latter being empty or only one pair with content (including spaces)
- `[this link][]`
- `[this link]`
Regexp notes:
- `(?:\[.+?\]){1}`: Always one bracket pair with content (including spaces)
- `(?:\[\]|\[.+?\])?`: Optional second pair that may or may not contain content (including spaces)
- `(?!:)`: Never followed by a `:` which is reserved for identifier definition syntax (`[identifier]: /the-link`)
- Each of the three parts is non-captured, but the match as a whole is captured
*/
const identifierInstanceRegex = /((?:\[.+?\]){1}(?:\[\]|\[.+?\])?(?!:))/g;
const isIdentifierInstance = (literal) => {
// Reset lastIndex as global flag in regexp are stateful
identifierInstanceRegex.lastIndex = 0;
return identifierInstanceRegex.test(literal);
};
const canRender = ({ literal }) => isIdentifierInstance(literal);
const tokenize = (text) => {
const matches = text.split(identifierInstanceRegex);
const tokens = matches.map((match) => {
const token = buildTextToken(match);
return isIdentifierInstance(match) ? buildUneditableInlineTokens(token) : token;
});
return tokens.flat();
};
const render = (_, { origin }) => tokenize(origin().content);
export default { canRender, render };
|