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.

@gridsome/source-filesystem for different content route format but same template/typeName

See original GitHub issue

Summary

I wonder if there is a way to allow @gridsome/source-filesystem to accepts content from many directory.

Basic example

Saying I organize my posts in blog directory and memo directory. All are markdown and I want to use the same template for them. I would like the route for blog posts are /blog/:slug and route for memo are /memo/:slug. How can it done with current plugin config?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tomtevcommented, Apr 17, 2019

Hi @eneim , you can add as many filesystem sources as you want. Take a look here for an example: https://github.com/gridsome/gridsome.org/blob/master/gridsome.config.js

0reactions
platform-kitcommented, Jun 13, 2021

Hi @rsclarke, @tomtev, @eneim

I found that the simplest solution to this problem is to manually assign the path in the Templates part of the gridsome config using a function

IE, for this plugin setup…

var plugins = [
 {
    use: '@gridsome/source-filesystem',
    options: {
      path: '**/*.md',
      baseDir: 'temp/docs',
      typeName: 'Doc'
    }
  }
];

and this function to generate the path…

function slugify(node) {
  var text = node.title;
  console.log(node);  
  var text = text.toString().toLowerCase()
    .replace(/\s+/g, '-')           // Replace spaces with -
    .replace(/[^\w\-]+/g, '')       // Remove all non-word chars
    .replace(/\-\-+/g, '-')         // Replace multiple - with single -
    .replace(/^-+/, '')             // Trim - from start of text
    .replace(/-+$/, '');            // Trim - from end of text
    return  node.fileInfo.directory + '/' + text;
}

and this Gridsome config that references the function in the templates object…

var gridsomeConfig = {
  siteName: siteName,
  icon: {
    favicon: favicon,
    touchicon: favicon
  },
  plugins: plugins,
  templates: {
    Doc:
      (node) => {
        return `/docs/${slugify(node)}/`
      }    
  }
}

and given some markdown files in various subdirectories, i.e. /api and /ui…

You will get content of type Doc at /docs/ui, /docs/api, etc. With the need for only one template (GraphQL type – meaning same query for all docs).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining Your Routes - Routing - Ember Guides
The application route is entered when your app first boots up. Like other routes, it will load a template with the same name...
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