Created
November 14, 2022 09:06
-
-
Save Arkellys/3971a732a9cd326c1a8a230135683ed7 to your computer and use it in GitHub Desktop.
Very simple remark plugins for tags, mentions and comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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