Skip to content

Commit becebb2

Browse files
Mister-Hopemeteorlxy
andauthoredApr 21, 2024··
feat(core): allow null permalink in frontmatter to disable permalink (#1547)
Co-authored-by: Xinyu Liu <meteor.lxy@foxmail.com>
1 parent d584ee3 commit becebb2

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed
 

‎packages/core/src/page/resolvePagePermalink.ts

+13-23
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@ export const resolvePagePermalink = ({
2020
pathInferred: string | null
2121
pathLocale: string
2222
}): string | null => {
23-
// use permalink in frontmatter directly
23+
// frontmatter.permalink has the highest priority
24+
if (frontmatter.permalink === null) {
25+
return null
26+
}
2427
if (isString(frontmatter.permalink)) {
2528
return frontmatter.permalink
2629
}
2730

28-
// get permalink pattern
29-
const permalinkPattern = getPermalinkPattern({ app, frontmatter })
30-
if (permalinkPattern === null) {
31+
// frontmatter.permalinkPattern has higher priority than app.options.permalinkPattern
32+
if (frontmatter.permalinkPattern === null) {
33+
return null
34+
}
35+
36+
const permalinkPattern =
37+
frontmatter.permalinkPattern || app.options.permalinkPattern
38+
39+
if (!isString(permalinkPattern)) {
3140
return null
3241
}
3342

@@ -45,22 +54,3 @@ export const resolvePagePermalink = ({
4554

4655
return ensureLeadingSlash(link)
4756
}
48-
49-
/**
50-
* Get permalink pattern from frontmatter or app options
51-
*/
52-
const getPermalinkPattern = ({
53-
app,
54-
frontmatter,
55-
}: {
56-
app: App
57-
frontmatter: PageFrontmatter
58-
}): string | null => {
59-
if (frontmatter.permalinkPattern === null) {
60-
return null
61-
}
62-
if (isString(frontmatter.permalinkPattern)) {
63-
return frontmatter.permalinkPattern
64-
}
65-
return app.options.permalinkPattern
66-
}

‎packages/core/tests/page/resolvePagePermalink.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ describe('core > page > resolvePagePermalink', () => {
3636
expect(resolved).toBe('/frontmatter')
3737
})
3838

39+
it('should return null', () => {
40+
const resolved = resolvePagePermalink({
41+
app: appWithPermalinkPattern,
42+
frontmatter: {
43+
permalink: null,
44+
},
45+
slug: '',
46+
date: '',
47+
pathInferred: '/inferred',
48+
pathLocale: '',
49+
})
50+
51+
expect(resolved).toBe(null)
52+
})
53+
3954
it('should use permalinkPattern in frontmatter', () => {
4055
const resolved = resolvePagePermalink({
4156
app,

‎packages/shared/src/types/page.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export type PageFrontmatter<
6262
head?: HeadConfig[]
6363
lang?: string
6464
layout?: string
65-
permalink?: string
65+
permalink?: string | null
6666
permalinkPattern?: string | null
6767
routeMeta?: Record<string, unknown>
6868
title?: string

0 commit comments

Comments
 (0)
Please sign in to comment.