In the Pug Language reference there is an easy example on how to use the markdown filter on a .md file and include it.
include:markdown-it myfile.md
However, I can't get to work reading from a file which name is in a variable. I expected this syntax to work:
include:markdown-it ${article}
Being var article = 'myfile.md'
. But the code crash saying it can't find file '${article}' in my views folder.
What is the correct way of doing it? Or is it just imposible?
Regards.
EDIT: As suggested here, it's not possible to do what I want. The solution I found was rendering the markdown file first,
function loadmd(md) {
var fs = require('fs');
var markdown = require('markdown-it')();
var mdfile = fs.readFileSync(__dirname + '/mds/' + md + '.md');
return markdown.render(mdfile.toString());
}
and then pass it to the pug renderer as a variable:
res.render('art', {md: loadmd(req.params.article)}, function renderDone(err, html) { ... }
Template:
doctype html
html(lang=es)
head
include htmlhead
title !{pagetitle}
body
include pageheader
include pagemenu
section
article !{md}
footer
include pagefooter