question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting layout in Front Matter

See original GitHub issue

It’s possible to set the layout inside the front matter of markdown files? Like this:

content/blog/hello-world.md

---
layout: 'layout-1'
title: Hello World
---

content/blog/featured-article.md

---
layout: 'featured-layout'
title: Article with special layout
---

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
NozomuIkutacommented, Oct 23, 2020

I also dug into this issue and here are what I found.

Firstly, as @mrhubbs pointed out, layout option must be either a string or a synchronous function that returns a string. Fetching the document is asynchronous, so modifying layout option can not be an option to achieve the goal.

Another way is, as described in this issue, to modify context in middleware function which is called before layout function.

I tried this approach and it seems working.

export default {
  layout ({ store, documentLayout }) {
    return documentLayout || store.state.settings.layout || 'default'
  },
  async middleware (context) {
    const { app, params, redirect, error, $content } = context
    const path = `/${app.i18n.locale}/${params.pathMatch || 'index'}`
    const [document] = await $content({ deep: true }).where({ path }).fetch()
    if (!document) {
      return error({ statusCode: 404, message: 'Page not found' })
    }

    context.document = document
    context.documentLayout = document.layout

    if (params.pathMatch === 'index') {
      redirect(app.localePath('/'))
    }
  },
  async asyncData ({ $content, document, app }) {
    const [prev, next] = await $content(app.i18n.locale, { deep: true })
      .only(['title', 'slug', 'to'])
      .sortBy('position', 'asc')
      .surround(document.slug, { before: 1, after: 1 })
      .fetch()

    return {
      document,
      prev,
      next
    }
  },
  // ...
}

I set document as well as documentLayout just because I thought fetching document twice in middleware and asyncData would be at higher cost.

Note that I just did a quick testing in my local:

  1. Run yarn create nuxt-content-docs test-layout.
  2. Run yarn link to replace installed content-theme-docs package with that in my local.
  3. Set layout: single in the front matter of setup.md file.
  4. Run yarn run dev, and check whether layout is changed dynamically when I move between index and setup pages.
  5. Run yarn run generate, yarn run start, and check whether layout is changed dynamically when I move between the 2 pages.
0reactions
farnabazcommented, Dec 12, 2022

Using document-driven mode in nuxt/content@v2 you can set layout name in front-matter.

I’m closing this because nuxt/content@v1 will not receive any update for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Front Matter - Hugo
Front matter allows you to keep metadata attached to an instance of a content type—i.e., embedded inside a content file—and is one of...
Read more >
Front Matter | Jekyll • Simple, blog-aware, static sites
If set, this specifies the layout file to use. Use the layout file name without the file extension. Layout files must be placed...
Read more >
Front Matter | Hugo - Mike Dane
This tutorial covers how to use front matter in Hugo - Static Site Generator. ... we can use elsewhere (like in layouts) to...
Read more >
Hugo change layout - Stack Overflow
Use front matter to set the "layout": In content/get-started.md set layout: mylayout in front matter. The layout will be located at (for ...
Read more >
Front-matter | Hexo
The default layout is post , in accordance to the value of default_layout setting in _config.yml . When the layout is disabled (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found