Skip to content

Instantly share code, notes, and snippets.

@yhatt
Last active March 10, 2024 02:00
Show Gist options
  • Save yhatt/13a9f52c96821ae34c3de5f732f305df to your computer and use it in GitHub Desktop.
Save yhatt/13a9f52c96821ae34c3de5f732f305df to your computer and use it in GitHub Desktop.
Marp hide slides plugin example
const marpHideSlidesPlugin = (md) => {
md.marpit.customDirectives.local.hide = (value) => ({
hide: value === 'true',
})
md.core.ruler.after(
'marpit_directives_apply',
'marpit_hide_slides',
(state) => {
let withinSlide = false
let isHideSlide = false
let slideStartIdx = 0
let slideEndIdx = 0
for (let i = 0; i < state.tokens.length; i += 1) {
const token = state.tokens[i]
if (!withinSlide && token.meta?.marpitSlideElement === 1) {
withinSlide = true
isHideSlide = false
slideStartIdx = i
}
if (withinSlide) {
if (token.meta?.marpitSlideElement === -1) {
withinSlide = false
slideEndIdx = i
if (isHideSlide) {
state.tokens.splice(
slideStartIdx,
slideEndIdx - slideStartIdx
)
i = slideStartIdx
}
} else if (
token.type === 'marpit_slide_open' &&
token.meta?.marpitDirectives.hide
) {
isHideSlide = true
}
}
}
}
)
}
module.exports = marpHideSlidesPlugin
Error in user YAML: (<unknown>): mapping values are not allowed in this context at line 4 column 34
---
marp: true
theme: default
paginate: true
style: section::after { font-size: xx-large; }
---

Marp hide slides plugin example

git clone https://gist.github.com/13a9f52c96821ae34c3de5f732f305df.git marp-hide-slides
cd ./marp-hide-slides
npm i
npx marp -c ./marp.config.js ./hide-slides.md -p

This page has been marked as hidden

<!-- _hide: true -->

Architecture

This plugin will strip slide tokens from rendered result if detected a definition of hide custom local directive in the slide.


Hide multiple pages

By using hide directive without underscore prefix for scoping, you can hide multiple pages at once.


hide custom local directive is also following a general rule of Marpit local directive.

bg right fit


Pagination

Marpit's pagination provided by paginate directive is keeping an original page number even if the slide page was hidden.


References

const marpHideSlidesPlugin = require('./hide-slides-plugin')
module.exports = {
engine: ({ marp }) => marp.use(marpHideSlidesPlugin)
}
{
"license": "WTFPL",
"private": true,
"dependencies": {
"@marp-team/marp-cli": "^2.1.2",
"@marp-team/marp-core": "^3.3.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment