Osmose / phantomake

A file-focused static site generator.
https://www.mkelly.me/phantomake/
ISC License
17 stars 1 forks source link

embedding ejs templates in markdown? #1

Closed Alloyed closed 6 days ago

Alloyed commented 1 week ago

here's my page: https://alloyed.me most of the content is a single file named "index.md", but I wanted to automatically generate the html for the button bar so I moved that into an ejs template and a json data file (let's call it .buttons.html.ejs and .buttons.json)

my first attempt was renaming index.md to index.md.ejs and including my templating code directly:

# website
blah blah blah
<%- include(".buttons.html", JSON.parse(include(".buttons.json") %>

this didn't work, my file was no longer recognized as the index of my site. to fix this I moved it into my default.ejs instead, outside of the content of my home page

<html>
<%- output.content %>
<%- include(".buttons.html", JSON.parse(include(".buttons.json") %>
</html>

It would have been nice if the first thing worked, though.

(also this is in no way your fault but the upstream EJS docs are really really bad! figuring out how to pull in a data file, and how the second parameter to include is used, was a whole ordeal)

Osmose commented 6 days ago

Thanks for the report!

Yeah, EJS vs markdown are both considered "processors" and only one can run on a given file. My main worry about allowing both to run on a file is that you either make it a special-case (EJS files that produce an .md file are then also processed as markdown) or you make it generic and infinitely nestable (EJS files that produce other EJS files that produce OTHER EJS files) and I think either of those goes beyond the level of complexity I want Phantomake's model of processing to have.

It's not a great alternative but the other option would be to use ctx.renderMarkdown to render a markdown string to HTML in the file (renaming it to index.html.ejs so that this outputs an index.html file for your site index):

<div>
  <%- include('.buttons.html', JSON.parse(include('.buttons.json')) %>
  <%- ctx.renderMarkdown(`
    hi it's me kit again

    # [links](https://example.com)

    it remains to be seen which of these I will actually be active on.

    - [cohost (RIP)](https://example.com)
    - [mastodon](https://example.com)
  `) %>
</div>

RE: EJS, that's a fair point and I wouldn't be opposed to adding an EJS primer / cheatsheet to the doc site. I'll keep it in mind.

As for the EJS in markdown, I probably won't add it, so I'll close this issue. Thanks for using Phantomake!