Created
March 24, 2026 09:36
-
-
Save gucheen/955020da4c80fcc0e14eded541de6200 to your computer and use it in GitHub Desktop.
RemarkShortcodesEvo.js
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 { visit } from 'unist-util-visit' | |
| /** | |
| * @import {Root} from 'mdast' | |
| */ | |
| export default function RemarkShortcodesEvo() { | |
| /** | |
| * @param {Root} tree | |
| * @return {undefined} | |
| */ | |
| return function (tree) { | |
| visit(tree, 'text', function(node) { | |
| if (node.value.includes('{{')) { | |
| node.value.matchAll(/\{\{.*\}\}/g).forEach(match => { | |
| const shortcode = match[0].substring(2, match[0].length - 2).trim() | |
| shortcode.matchAll(/(\w+)\((.*)\)/g).forEach(match2 => { | |
| const shortcodeName = match2[1] | |
| const shortcodeArgs = match2[2].split(',').map((arg) => { | |
| const argSet = arg.split('=') | |
| const key = argSet[0].trim() | |
| let value = argSet[1].trim() | |
| if (!value.startsWith('"')) { | |
| value = `"${value}"` | |
| } | |
| if (!value.endsWith('"')) { | |
| value = `${value}"` | |
| } | |
| return `${key}=${value}` | |
| }) | |
| node.value = node.value.replace(match[0], `::${shortcodeName}{${shortcodeArgs.join(' ')}} | |
| ::`) | |
| }) | |
| }) | |
| } | |
| }) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment