Skip to content

Instantly share code, notes, and snippets.

@Arkellys
Created November 14, 2022 09:06
Show Gist options
  • Select an option

  • Save Arkellys/3971a732a9cd326c1a8a230135683ed7 to your computer and use it in GitHub Desktop.

Select an option

Save Arkellys/3971a732a9cd326c1a8a230135683ed7 to your computer and use it in GitHub Desktop.
Very simple remark plugins for tags, mentions and comment.
import { findAndReplace } from "mdast-util-find-and-replace";
const commentRegex = /\/\/([^\n\\/]*)\/\//g; // //<value>//
export default function () {
function replaceComment(_match, text) {
return {
type: "span",
data: {
hName: "comment",
hProperties: { text }
}
};
}
function transformer(tree) {
findAndReplace(tree, commentRegex, replaceComment);
}
return transformer;
}
import { findAndReplace } from "mdast-util-find-and-replace";
const mentionRegex = /@([^\n(]+)[(]([A-Z0-9]{5})?[)]/g; // @<value>(<5-characters-id>)
export default function () {
function replaceMention(_match, alias, id = "00000") {
return {
type: "span",
data: {
hName: "mention",
hProperties: { alias, id }
}
};
}
function transformer(tree) {
findAndReplace(tree, mentionRegex, replaceMention);
}
return transformer;
}
import { findAndReplace } from "mdast-util-find-and-replace";
const tagRegex = /#(\S+)/g; // #<value>
export default function () {
function replaceTag(_match, label) {
const cleanLabel = label.replace(/_/g, " "); // Supports multi-word tag
return {
type: "span",
data: {
hName: "tag",
hProperties: { label: cleanLabel }
}
};
}
function transformer(tree) {
findAndReplace(tree, tagRegex, replaceTag);
}
return transformer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment