MCPcopy Create free account
hub / github.com/editablejs/editable / getStaticPaths

Function getStaticPaths

apps/docs/src/pages/[[...markdownPath]].js:196–250  ·  view source on GitHub ↗
(context)

Source from the content-addressed store, hash-verified

194
195// Collect all MDX files for static generation.
196export async function getStaticPaths(context) {
197 const { promisify } = require('util')
198 const { resolve } = require('path')
199 const fs = require('fs')
200 const readdir = promisify(fs.readdir)
201 const stat = promisify(fs.stat)
202 const rootDir = process.cwd() + '/src/content'
203
204 // Find all MD files recursively.
205 async function getFiles(dir) {
206 const subdirs = await readdir(dir)
207 const files = await Promise.all(
208 subdirs.map(async subdir => {
209 const res = resolve(dir, subdir)
210 return (await stat(res)).isDirectory() ? getFiles(res) : res.slice(rootDir.length + 1)
211 }),
212 )
213 return files.flat().filter(file => file.endsWith('.md'))
214 }
215
216 // 'foo/bar/baz.md' -> ['foo', 'bar', 'baz']
217 // 'foo/bar/qux/index.md' -> ['foo', 'bar', 'qux']
218 function getSegments(file) {
219 const locale = context.locales.find(locale => file.endsWith(`.${locale}.md`))
220 let segments = file
221 .slice(0, (locale ? -locale.length - 1 : 0) - 3)
222 .replace(/\\/g, '/')
223 .split('/')
224
225 if (segments[segments.length - 1] === 'index') {
226 segments.pop()
227 }
228 return [segments, locale]
229 }
230
231 const files = await getFiles(rootDir)
232
233 const paths = files.map(file => {
234 const [paths, locale = 'en-US'] = getSegments(file)
235 return {
236 params: {
237 markdownPath: paths,
238 // ^^^ CAREFUL HERE.
239 // If you rename markdownPath, update patches/next-remote-watch.patch too.
240 // Otherwise you'll break Fast Refresh for all MD files.
241 },
242 locale,
243 }
244 })
245
246 return {
247 paths: paths,
248 fallback: false,
249 }
250}

Callers

nothing calls this directly

Calls 2

getFilesFunction · 0.85
getSegmentsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…