A barebones shortcode parser that handles edge cases.
npm install @tryghost/mg-shortcodesA barebones shortcode parser that handles edge cases.
npm install @tryghost/mg-shortcodes --save
or
yarn add @tryghost/mg-shortcodes
``js
// Import the class
import Shortcodes from '@tryghost/mg-shortcodes';
// Create a new instance
const shortcodes = new Shortcodes();
// Handle [link] shortcodes
shortcodes.add('link', ({attrs, content}) => {
return ${content};
});
// [abc color="red"]
Full post
[def]Free excerpt
[/abc]Full post
// Unwrap [block] shortcodes
shortcodes.unwrap('block');
// You can add & unwrap as many shortcodes as you like. The order is not important.
const content = [block][link url="https://example.com"]Hello[/link][/block];
// Parse the content and return the updated content
const updatedContent = shortcodes.parse(content);
// Hello
`
This is a mono repository, managed with lerna.
Follow the instructions for the top-level repo.
1. git clone this repo & cd into it as usualyarn
2. Run to install top-level dependencies.
- yarn dev
- yarn lint run just eslintyarn test
- run lint and testsyarn benchmark` run benchmarks
-
Copyright (c) 2013-2026 Ghost Foundation - Released under the MIT license.