forked from cory/tildefriends
.gitea
apps
admin
api
apps
blog
db
follow
identity
intro
issues
journal
room
sneaker
ssb
app.js
commonmark-hashtag.js
commonmark.min.js
emojis.js
emojis.json
filesaver.min.js
filesaver.min.js.map
index.html
lit-all.min.js
lit-all.min.js.map
script.js
tf-app.js
tf-compose.js
tf-message.js
tf-news.js
tf-profile.js
tf-reactions-modal.js
tf-styles.js
tf-tab-connections.js
tf-tab-news-feed.js
tf-tab-news.js
tf-tab-query.js
tf-tab-search.js
tf-tag.js
tf-user.js
tf-utils.js
tribute.css
tribute.esm.js
storage
test
todo
web
welcome
wiki
admin.json
api.json
apps.json
blog.json
db.json
follow.json
identity.json
intro.json
issues.json
journal.json
room.json
sneaker.json
ssb.json
storage.json
test.json
todo.json
web.json
welcome.json
wiki.json
core
deps
docs
metadata
src
tools
.clang-format
.dockerignore
.git-blame-ignore-revs
.gitignore
.gitmodules
.prettierignore
.prettierrc.yaml
CONTRIBUTING.md
Dockerfile
Doxyfile
GNUmakefile
LICENSE
README.md
default.nix
flake.lock
flake.nix
package-lock.json
package.json
95 lines
1.9 KiB
JavaScript
95 lines
1.9 KiB
JavaScript
function textNode(text) {
|
|
const node = new commonmark.Node('text', undefined);
|
|
node.literal = text;
|
|
return node;
|
|
}
|
|
|
|
function linkNode(text, link) {
|
|
const linkNode = new commonmark.Node('link', undefined);
|
|
if (link.startsWith('#')) {
|
|
linkNode.destination = `#${encodeURIComponent(link)}`;
|
|
} else {
|
|
linkNode.destination = link;
|
|
}
|
|
linkNode.appendChild(textNode(text));
|
|
return linkNode;
|
|
}
|
|
|
|
function splitMatches(text, regexp) {
|
|
// Regexp must be sticky.
|
|
regexp = new RegExp(regexp, 'gm');
|
|
|
|
let i = 0;
|
|
const result = [];
|
|
|
|
let match = regexp.exec(text);
|
|
while (match) {
|
|
const matchText = match[0];
|
|
|
|
if (match.index > i) {
|
|
result.push([text.substring(i, match.index), false]);
|
|
}
|
|
|
|
result.push([matchText, true]);
|
|
i = match.index + matchText.length;
|
|
|
|
match = regexp.exec(text);
|
|
}
|
|
|
|
if (i < text.length) {
|
|
result.push([text.substring(i, text.length), false]);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
const regex = new RegExp('(?:https?://[^ ]+[^ .,])|(?:(?<!\\w)#[\\w-]+)|(?:@[A-Za-z0-9+/]+=.ed25519)|(?:[%&][A-Za-z0-9+/]+=.sha256)');
|
|
|
|
function split(textNodes) {
|
|
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]);
|
|
}
|
|
});
|
|
}
|
|
|
|
export function transform(parsed) {
|
|
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;
|
|
}
|