Skip to content

Commit 4fb075a

Browse files
committed
feat: Default slug to notion-page id
1 parent 03aad7a commit 4fb075a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ One of the big attractions of Notion for large documentation projects is that yo
7979

8080
![image](https://user-images.githubusercontent.com/8448/168929668-f83d7c86-75d2-48e9-940c-84c5268a2854.png)
8181

82+
## Slugs
83+
84+
By default, pages will be given a slug based on the Notion id. For a human-readable URL, add a notion property named `Slug` and enter values in there.
85+
8286
## Known Limitations
8387

8488
docu-notion is not doing anything smart with regards to previously Published but now not Published documents. All it does is ignore every Notion document that doesn't have `status == Publish`. So if the old version of the document is still in your file tree when your static site generator (e.g. Docusaurus) runs, then it will appear on your website. If it isn't there, it won't. If you rename directories or move the document, docu-notion will not realize this and will delete the previously published markdown file.

src/NotionPage.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,40 @@ export class NotionPage {
100100
private get name(): string {
101101
return this.getPlainTextProperty("Name", "name missing");
102102
}
103-
public get slug(): string {
103+
104+
private explicitSlug(): string | undefined {
104105
const explicitSlug = this.getPlainTextProperty("Slug", "");
105-
if (explicitSlug) return explicitSlug;
106-
return encodeURIComponent(this.nameOrTitle.replaceAll(" ", "-"))
107-
.replaceAll("%3A", "-")
108-
.replaceAll("--", "-");
106+
if (explicitSlug) {
107+
if (explicitSlug === "/") return explicitSlug; // the root page
108+
else
109+
return (
110+
"/" +
111+
encodeURIComponent(
112+
explicitSlug
113+
.replace(/^\//, "")
114+
// If for some reason someone types in a slug with special characters,
115+
//we really don't want to see ugly entities in the URL, so first
116+
// we replace a bunch of likely suspects with dashes. This will not
117+
// adequately handle the case where there is one pag with slug:"foo-bar"
118+
// and another with "foo?bar". Both will come out "foo-bar"
119+
.replaceAll(" ", "-")
120+
.replaceAll("?", "-")
121+
.replaceAll("/", "-")
122+
.replaceAll("#", "-")
123+
.replaceAll("&", "-")
124+
.replaceAll("%", "-")
125+
// remove consecutive dashes
126+
.replaceAll("--", "-")
127+
)
128+
);
129+
return undefined; // this page has no slug property
130+
}
109131
}
132+
133+
public get slug(): string {
134+
return this.explicitSlug() ?? "/" + this.pageId;
135+
}
136+
110137
public get keywords(): string | undefined {
111138
return this.getPlainTextProperty("Keywords", "");
112139
}

0 commit comments

Comments
 (0)