Compile Markdown as Vue template on Nuxt.js dynamically

Every static site content is by Markdown

Kengo Hamasaki / hmsk
Hai-iro
3 min readMar 5, 2018

--

For implementing data pipeline for the static website, I used to have Markdown files for content parts from long time ago. Probably starting with Jekyll, then Middleman, Harp and Nuxt.js right now.

Also about current project, decided to accept HTML on Markdown. And an additional spice, I provide expressive appearances with Vue component on Markdown.

If this original-component works as Vue component, could do a lot of things, right?

However, it was a bit tricky on Nuxt.js. This template section on Vue compoent was the base idea to render Markdown on Vue component:

renderedMarkdown is given as HTML compiled from Markdown data from JS side. This was completely straight forward, but couldn’t run as I expected unfortunately. The HTML on Markdown does not work as Vue template/component.

original-component will be just rendered as plain HTML 😢

As a matter of course, the content of v-html is not compiled as Vue’s component dynamically. So requires to compile the HTML in runtime.

After some experiments/investigations, I found out this codes are minimum changes for realizing that. The most of the results are come from this thread actually.

Parent Component

Compiles markdown and passes to the no-ssr-ed component to evaluate as Vue template.

  • <no-ssr> is required to compile the HTML dynamically
  • Compiled markdown should be allowed having HTML

Child Component

Use render function by Vue.compile

  • Compile is bundled in vue/dist/vue
    📝 Runtime + Compiler vs. Runtime-only
  • Register components which are called in Markdown such like OriginalComponent
  • Don’t have template tag as SFC
  • Compile passed a prop as Vue’s template (which should be single root elemnt 😉)

Notes

Bundle size is increased by importing “Full-build Vue”

Since the runtime-only builds are roughly 30% lighter-weight than their full-build counterparts, you should use it whenever you can.

I don’t think this difference will be significant rather other contents like images 😉 could be acceptable as static generated site.

Dynamic part couldn’t be pre-rendered

Should care about SEO for visitor without JS execution

Markdown should be rendered as template of Vue component before running Vue basically?

Probably, so. If my approach didn’t work, I would take that way. Although, I preferred to handle Markdown as the state on Vuex as possible, for comfortable development speed. didn’t want to have process to compile markdown avoiding HMR with Nuxt.js.

--

--