Skip to content

Instantly share code, notes, and snippets.

@gucheen
Created March 24, 2026 09:36
Show Gist options
  • Select an option

  • Save gucheen/955020da4c80fcc0e14eded541de6200 to your computer and use it in GitHub Desktop.

Select an option

Save gucheen/955020da4c80fcc0e14eded541de6200 to your computer and use it in GitHub Desktop.
RemarkShortcodesEvo.js
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