nuxt / content

The file-based CMS for your Nuxt application, powered by Markdown and Vue components.
https://content.nuxt.com
MIT License
3.06k stars 621 forks source link

Surround for articles organized as directories #927

Closed fuwuqi59 closed 3 years ago

fuwuqi59 commented 3 years ago

Hi I've stumbled upon the following issue and would love to hear your thoughts. I have a structure for my blog like the following:

content/ articles/ article-1/ index.md article-2/ index.md

I had no issues up to the point where I was trying to fetch previous and next articles surrounding a specific slug. I faced no issues if my md articles are organized using a file structure i.e. article-1.md (using the following code), but when they are reorganized as directory, that's when I failed to fetch surrounding articles.

const [prev, next] = await this.$content('articles') .only(['title', 'path']) .sortBy('date') .surround('article-2') .fetch()

How should I proceed when trying to fetch the surrounding articles organized as directories?

ManasMadrecha commented 3 years ago

Hi, You will have to use the route's path inside the surround query.

Instead of having a site-wide i18n, I wanted i18n for specific articles, so I recently restructured the content folder of this repo https://github.com/jainism-portal/jain-aagam with each deeply nested article present in its namesake language folder, instead of the article directly being a file. You can check its content folder's structure for reference.

Earlier, the structure was:

๐Ÿ“ en
  ๐Ÿ“ folder... more folders
    ๐Ÿงพ apple
๐Ÿ“ hi
  ๐Ÿ“ folder... more folders
    ๐Ÿงพ apple
    ๐Ÿงพ mango

Here, on the apple route, it is easy to fetch prev and next articles by using either slug or path in the surround because that will directly give you the mango md file.

But, now, the structure is

๐Ÿ“ folder
  ๐Ÿ“ folder... more folders
      ๐Ÿ“ apple
        ๐Ÿงพ meta
        ๐Ÿงพ en
        ๐Ÿงพ hi
      ๐Ÿ“ mango
        ๐Ÿงพ meta
        ๐Ÿงพ hi

Here, on the apple route wherein I display meta info along with the current language's body, I cannot simply use path in the surround query. It will give me meta, en, hi md files of the apple folder itself, which I don't want.

So, I have to modify it like this .surround(`/folder${this.filePathWithoutLocale}/meta`)

So, for the apple route, mango folder's meta file will be fetched as the next article.


You can check it here https://github.com/jainism-portal/jainaagam/blob/506db64b2c3a7c019bb38d218b859fc23bf48980/components/organisms/post/PostPrevNext.vue#L28-L42

Here, the prop filePathWithoutLocale is being sent to the PostPrevNext component from inside the _.vue page's computed properties. Basically, it is path itself, without the suffix locale. You can check it here https://github.com/jainism-portal/jainaagam/blob/506db64b2c3a7c019bb38d218b859fc23bf48980/pages/_.vue#L234-L240

NozomuIkuta commented 3 years ago

@fuwuqi59

surround() method accepts only slug or path of a document (link).


Every element of the result array is a document object so there is nothing corresponding to directories. It's just a part of paths of documents.

More accurately, What surround() method does is (1) to find it from the result array that the first element whose slug or path matches with the first argument of surround() method, (2) to make up a new array by slicing the result array, and (3) to return it.

In this way, Nuxt Content can't find document by directory name, but it needs a cue by which to specify a document.