Skip to content

Commit

Permalink
Add handlebars variable to disable handlebars
Browse files Browse the repository at this point in the history
When writing about ts-in-js, JSDoc blocks look unfortunately like handelbars templates. This lets you easily disable and enable them on a per page basis.
  • Loading branch information
bcomnes committed Nov 23, 2023
1 parent 3422a0f commit 789013d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ src/page-name/loose-md.md
- `md` pages can have YAML frontmatter, with variables that are accessible to the page layout and handlebars template blocks when building.
- You can include html in markdown files, so long as you adhere to the allowable markdown syntax around html tags.
- `md` pages support [handlebars][hb] template placeholders.
- You can disbale `md` page [handlebars][hb] processing by setting the `handlebars` variable to `false`.
- `md` pages support many [github flavored markdown features](https://github.com/bcomnes/siteup/blob/6481bd01e59e5d8a4bfcb33008f44a1405bf622b/lib/build-pages/page-builders/md/get-md.js#L25-L36).

An example of a `md` page:
Expand Down Expand Up @@ -147,6 +148,7 @@ src/page-name/page.html
- `html` pages are the simplest page type in `top-bun`. They let you build with raw html for when you don't want that page to have access to markdown features. Some pages are better off with just raw `html`.
- `html` page variables can only be set in a `page.vars.js` file inside the page directory.
- `html` pages support [handlebars][hb] template placeholders.
- You can disbale `html` page [handlebars][hb] processing by setting the `handlebars` variable to `false`.

An example `html` page:

Expand Down
9 changes: 7 additions & 2 deletions lib/build-pages/page-builders/html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ export async function htmlBuilder ({ pageInfo }) {
return {
vars: {},
pageLayout: async (vars) => {
const template = Handlebars.compile(fileContents)
return template(vars)
// @ts-ignore
if (vars?.handlebars) {
const template = Handlebars.compile(fileContents)
return template(vars)
} else {
return fileContents
}
}
}
}
12 changes: 9 additions & 3 deletions lib/build-pages/page-builders/md/get-md.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ export function getMd () {
*/
export function renderMd (mdUnparsed, vars, md) {
if (!md) md = getMd()
const template = Handlebars.compile(mdUnparsed)
const body = rewriteLinks(md.render(template(vars)))
return body
// @ts-ignore
if (vars?.handlebars) {
const template = Handlebars.compile(mdUnparsed)
const body = rewriteLinks(md.render(template(vars)))
return body
} else {
const body = rewriteLinks(md.render(mdUnparsed))
return body
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/build-pages/page-builders/md/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export async function mdBuilder ({ pageInfo }) {
mdUnparsed = fileContents
}

const body = renderMd(mdUnparsed, frontMatter, md)
const body = renderMd(mdUnparsed, { handlebars: false, ...frontMatter }, md)
const title = cheerio.load(body)('h1').first().text().trim()

return {
Expand Down
3 changes: 2 additions & 1 deletion lib/defaults/default.vars.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default {
layout: 'root',
siteName: 'top-bun website'
siteName: 'top-bun website',
handlebars: true
}

0 comments on commit 789013d

Please sign in to comment.