-
I am looking for a way to use the frontmatter (with some custom fields) to provide simple strings that would be templated into the rendered slides. It would be useful to be able to use some of the existing frontmatter fields like title and author, plus anything else that is added into the frontmatter and be able to use them to template sections through out the slide deck. e.g. ---
marp: true
title: My Presentation
subtitle: Getting Started in Life
author: Sean
---
<!-- _class: lead -->
<!-- size: 16:9 -->
# {{ title }}
## {{ subtitle }}
##### Presented by {{ author }}
--- Is there already a reasonably easy way to do this? I'd prefer not to introduce a new tool at the moment, but I am open to suggestions if that is the only option. Thanks! Sean |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
It could be achieved with making a plugin for Marp(it) with following logic:
Tip Marpit will recognize only pre-registered directives while parsing Markdown. So it might be better to use the nested object definition within the pre-registered custom global directive if you want to allow the custom placeholder name in the Markdown document. ---
meta:
foo: xxx
bar: xxx
--- marpit.customDirectives.global.meta = (value) =>
typeof value === 'object' ? { meta: value } : {} |
Beta Was this translation helpful? Give feedback.
-
Thank you @yhatt. That is helpful. Although I do some development, I am very new to Javascript and Marp's codebase, so I am very slowly make sense of it all. At the moment, I have a very simple use case started just using the engine file (without a plugin) like so: const { Marp } = require('@marp-team/marp-core')
const marpOpts = {
html: true,
}
function getFullTimestamp() {
const pad = (n, s = 2) => (`${new Array(s).fill(0)}${n}`).slice(-s);
const d = new Date();
return `${pad(d.getFullYear(), 4)}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}`;
}
module.exports = marpOpts => {
const md = new Marp(marpOpts)
md.customDirectives.global.author = (value) => {
return { author: "Me" }
}
const proxy = (tokens, idx, options, env, self) => md.markdown.renderer.renderToken(tokens, idx, options);
const defaultTextRenderer = md.markdown.renderer.rules.text || proxy;
const timestamp = getFullTimestamp()
md.markdown.renderer.rules.text = (tokens, idx, options, env, self) => {
let text = tokens[idx].content;
let step2 = text.replace(/{{{AUTHOR}}}/g, "TEST"); // How to get the value of the globalDirective?
tokens[idx].content = step2.replace(/{{{TIMESTAMP}}}/g, timestamp);
return defaultTextRenderer(tokens, idx, options, env, self)
};
return md
} I am struggling with getting the string value ( Any help/pointers would be greatly appreciated. Thank you! Sean |
Beta Was this translation helpful? Give feedback.
It could be achieved with making a plugin for Marp(it) with following logic:
{{ xxx }}
and replace with the custom placeholder tokenmarpit_directives_global_parse
core rule), to replace the content of custom placeholder tokens with the value of global directivesTip
Marpit will recognize only pre-registered directives while parsing Markdown. So it might be better to use the nested object definition within the pre-registered custom global directive if you want to allow the custom placeho…