Skip to content

Commit 77ac630

Browse files
authored
Normalized paths (#12)
* normalized paths * run-all package added * added postcss.config.js if package which uses this template still use lower version of tailwind * removed postcss and updated vitest dependencies * small update with helper functions in content-collections.ts * restored previous helper functions
1 parent dd0c787 commit 77ac630

File tree

5 files changed

+1150
-20
lines changed

5 files changed

+1150
-20
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,13 @@ pnpm install
9797
```
9898
3. Read through the README.md files in the project to understand our decisions.
9999

100-
4. Add `content` folder
100+
4. Run `pnpm run generate:docs` script
101101

102-
5. Run `pnpm run generate:docs` script
103-
104-
6. Start the development server:
102+
5. Start the development server:
105103
```bash
106104
pnpm run dev
107105
```
106+
107+
6. After you see that everything works with the current content inside the `content` folder, remove those files and add your own
108+
108109
7. Happy coding!

app/utils/load-content-collections.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { resolve } from "node:path"
1+
import path, { resolve } from "node:path"
2+
import { pathToFileURL } from "node:url"
23
import type { Page } from "content-collections"
34
import type { Section } from "content-collections"
45
import type { Version } from "./version-resolvers"
@@ -11,11 +12,14 @@ import type { Version } from "./version-resolvers"
1112
*/
1213
export async function loadContentCollections(version: Version) {
1314
const projectRoot = process.cwd()
14-
1515
const genBase = resolve(projectRoot, "generated-docs", version, ".content-collections", "generated")
1616

17-
const pagesMod = await import(/* @vite-ignore */ `${genBase}/allPages.js`)
18-
const sectionsMod = await import(/* @vite-ignore */ `${genBase}/allSections.js`)
17+
const pagesPath = pathToFileURL(path.join(genBase, "allPages.js")).href
18+
19+
const sectionsPath = pathToFileURL(path.join(genBase, "allSections.js")).href
20+
21+
const pagesMod = await import(/* @vite-ignore */ pagesPath)
22+
const sectionsMod = await import(/* @vite-ignore */ sectionsPath)
1923

2024
const allPages = pagesMod.default as Page[]
2125
const allSections = sectionsMod.default as Section[]

content-collections.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { posix } from "node:path"
12
import { defineCollection, defineConfig } from "@content-collections/core"
23
import { compileMDX } from "@content-collections/mdx"
34
import rehypeSlug from "rehype-slug"
@@ -39,12 +40,19 @@ export type Page = z.infer<typeof pageOutputSchema>
3940
/**
4041
* Removes leading number prefixes like "01-", "02-" from each path segment.
4142
*/
42-
const cleanSlug = (path: string) =>
43-
path
43+
const cleanSlug = (p: string) =>
44+
toPosix(p)
4445
.split("/")
46+
.filter(Boolean)
4547
.map((seg) => seg.replace(/^\d{2,}-/, ""))
4648
.join("/")
4749

50+
const toPosix = (s: string) => {
51+
return posix.normalize(s.replace(/\\/g, "/"))
52+
}
53+
const stripExt = (s: string) => s.replace(/\.(md|mdx)$/i, "")
54+
const stripTrailingIndex = (s: string) => s.replace(/\/index$/i, "")
55+
4856
/*
4957
* This collection defines a documentation section shown in the sidebar of the package documentation.
5058
*
@@ -57,8 +65,7 @@ const section = defineCollection({
5765
include: "**/index.md",
5866
schema: sectionSchema,
5967
transform: (document) => {
60-
const relativePath = document._meta.path.split("/").filter(Boolean).join("/")
61-
const slug = cleanSlug(relativePath)
68+
const slug = stripTrailingIndex(cleanSlug(document._meta.path))
6269
return { ...document, slug }
6370
},
6471
})
@@ -74,14 +81,11 @@ const page = defineCollection({
7481
include: "**/**/*.mdx",
7582
schema: pageSchema,
7683
transform: async (document, context) => {
77-
const relativePath = document._meta.path.split("/").filter(Boolean).join("/")
78-
const slug = cleanSlug(relativePath)
79-
const content = await compileMDX(context, document, {
80-
rehypePlugins: [rehypeSlug],
81-
})
82-
83-
// rawMdx is the content without the frontmatter, used to read headings from the mdx file and create a content tree for the table of content component
84+
const cleanedSlug = cleanSlug(document._meta.path)
85+
const slug = stripExt(cleanedSlug)
86+
const content = await compileMDX(context, document, { rehypePlugins: [rehypeSlug] })
8487
const rawMdx = document.content.replace(/^---\s*[\r\n](.*?|\r|\n)---/, "").trim()
88+
8589
return {
8690
...document,
8791
content,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"clean": "git clean -fdX --exclude=\"!.env\"",
1212
"script": "tsx scripts/setup.ts",
1313
"build": "react-router build",
14-
"predev": "pnpm run typegen && pnpm run verify:docs",
14+
"predev": "run-s typegen verify:docs",
1515
"dev": "react-router dev",
1616
"start": "NODE_ENV=production node ./build/server/index.js",
1717
"pretest": "pnpm run typegen",
@@ -81,6 +81,7 @@
8181
"happy-dom": "16.8.1",
8282
"knip": "5.43.6",
8383
"lefthook": "1.10.10",
84+
"npm-run-all": "4.1.5",
8485
"playwright": "1.50.1",
8586
"prompt": "1.3.0",
8687
"react-router-devtools": "5.0.4",

0 commit comments

Comments
 (0)