Content: How to exclude/ignore README.md from $content() fetches?

Created on 18 Nov 2020  Â·  3Comments  Â·  Source: nuxt/content

I'm wondering if I can tell @nuxt/content to ignore README files when "fetching" data in a cms like manner.

this.$content('menues') and http://localhost:3030/_content/menues both also return the README.md in ~/content/menues/

I don't want that.
I'd like to exclude README files in general for all content fetches.

question

Most helpful comment

@katerlouis

where() query-building method is for you (doc).

Each file name is used as a built-in slug property of document in database (doc), which means it can be filtered out by where() method like the following:

$content().where({ slug: { $ne: 'README' } }).fetch()

If you want to fetch all the README.md files only from all the sub directories, you can also do it:

$content({ deep: true }).where({ slug: 'README' }).fetch()
// or
$content({ deep: true }).where({ slug: { $eq: 'README' } }).fetch()

JFYI:

Actually, you can chain query-building methods before calling fetch(), so that you can fetch only documents which match all the queries built in the chain.

For example, here is how Theme Docs fetches prev/next documents of the current page's document (code):

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()

All 3 comments

@katerlouis

where() query-building method is for you (doc).

Each file name is used as a built-in slug property of document in database (doc), which means it can be filtered out by where() method like the following:

$content().where({ slug: { $ne: 'README' } }).fetch()

If you want to fetch all the README.md files only from all the sub directories, you can also do it:

$content({ deep: true }).where({ slug: 'README' }).fetch()
// or
$content({ deep: true }).where({ slug: { $eq: 'README' } }).fetch()

JFYI:

Actually, you can chain query-building methods before calling fetch(), so that you can fetch only documents which match all the queries built in the chain.

For example, here is how Theme Docs fetches prev/next documents of the current page's document (code):

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()

Thanks a lot!
I knew about the chaining and figured this could be done with .where(), but didn't quite get how to say "where slug NOT equals 'README'" – gotta google these mongo shorthands.

Now to the master question: Is there a way to make this the default behavior, so I don't need to write this all the time? I don't see why I would ever fetch the README on this site.

What about this in the nuxt-config:

content: {
  excludeSlugs: ['README'],
}

@katerlouis

Unfortunately, there is no such native option to achieve your goal. Actually, I created an issue (#595) to request such feature. As a result of discussion, draft: true/false flag at document level is introduced to Theme Docs, by the author (commit).

You may be able to achieve your goal by wrapping $content() by yourself in some way.

Here is a demo app, in which $content is wrapped by another function and the wrapper is injected to context via plugin system of Nuxt.

Caveat: Not-well tested, just a proof of concept.


By the way, I agree with your idea which matches DRY principle.
It would be nice of Nuxt Content to provide a way to create _instances_ like axios does.

Was this page helpful?
0 / 5 - 0 ratings