2022-11-11 22:06:29 -05:00
|
|
|
function textNode(text) {
|
2024-04-03 19:18:39 -04:00
|
|
|
const node = new commonmark.Node('text', undefined);
|
|
|
|
node.literal = text;
|
|
|
|
return node;
|
2022-11-11 22:06:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function linkNode(text, link) {
|
2024-04-03 19:18:39 -04:00
|
|
|
const linkNode = new commonmark.Node('link', undefined);
|
|
|
|
if (link.startsWith('#')) {
|
|
|
|
linkNode.destination = `#q=${encodeURIComponent(link)}`;
|
|
|
|
} else {
|
|
|
|
linkNode.destination = link;
|
|
|
|
}
|
|
|
|
linkNode.appendChild(textNode(text));
|
|
|
|
return linkNode;
|
2022-11-11 22:06:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function splitMatches(text, regexp) {
|
2024-04-03 19:18:39 -04:00
|
|
|
// Regexp must be sticky.
|
|
|
|
regexp = new RegExp(regexp, 'gm');
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
let i = 0;
|
|
|
|
const result = [];
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
let match = regexp.exec(text);
|
|
|
|
while (match) {
|
|
|
|
const matchText = match[0];
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
if (match.index > i) {
|
|
|
|
result.push([text.substring(i, match.index), false]);
|
|
|
|
}
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
result.push([matchText, true]);
|
|
|
|
i = match.index + matchText.length;
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
match = regexp.exec(text);
|
|
|
|
}
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
if (i < text.length) {
|
|
|
|
result.push([text.substring(i, text.length), false]);
|
|
|
|
}
|
2022-11-11 22:06:29 -05:00
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
return result;
|
2022-11-11 22:06:29 -05:00
|
|
|
}
|
|
|
|
|
2024-04-03 19:18:39 -04:00
|
|
|
const regex = new RegExp('(?:https?://[^ ]+[^ .,])|(?:(?<!\\w)#[\\w-]+)|(?:@[A-Za-z0-9+/]+=.ed25519)|(?:[%&][A-Za-z0-9+/]+=.sha256)');
|
2022-11-11 22:06:29 -05:00
|
|
|
|
|
|
|
function split(textNodes) {
|
2024-04-03 19:18:39 -04:00
|
|
|
const text = textNodes.map((n) => n.literal).join('');
|
|
|
|
const parts = splitMatches(text, regex);
|
|
|
|
|
|
|
|
return parts.map((part) => {
|
|
|
|
if (part[1]) {
|
|
|
|
return linkNode(part[0], part[0]);
|
|
|
|
} else {
|
|
|
|
return textNode(part[0]);
|
|
|
|
}
|
|
|
|
});
|
2022-11-11 22:06:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function transform(parsed) {
|
2024-04-03 19:18:39 -04:00
|
|
|
const walker = parsed.walker();
|
|
|
|
let event;
|
|
|
|
|
|
|
|
let nodes = [];
|
|
|
|
while ((event = walker.next())) {
|
|
|
|
const node = event.node;
|
|
|
|
if (event.entering && node.type === 'text') {
|
|
|
|
nodes.push(node);
|
|
|
|
} else {
|
|
|
|
if (nodes.length > 0) {
|
|
|
|
split(nodes)
|
|
|
|
.reverse()
|
|
|
|
.forEach((newNode) => {
|
|
|
|
nodes[0].insertAfter(newNode);
|
|
|
|
});
|
|
|
|
|
|
|
|
nodes.forEach((n) => n.unlink());
|
|
|
|
nodes = [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nodes.length > 0) {
|
|
|
|
split(nodes)
|
|
|
|
.reverse()
|
|
|
|
.forEach((newNode) => {
|
|
|
|
nodes[0].insertAfter(newNode);
|
|
|
|
});
|
|
|
|
nodes.forEach((n) => n.unlink());
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
2022-11-11 22:06:29 -05:00
|
|
|
}
|