Skip to content

Commit b6d1105

Browse files
sarahsCopilot
andauthored
Allow filename to match short title (#56673)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5f60d48 commit b6d1105

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/content-render/scripts/reconcile-filenames-with-ids.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,25 @@ contentFiles.forEach((oldFullPath) => {
3838
// skip pages with frontmatter flag
3939
if (data.allowTitleToDifferFromFilename) return
4040

41-
// slugify the title of each article
42-
// where title = Foo bar
43-
// and slug = foo-bar
41+
// Slugify the title of each article, where:
42+
// * title = Foo bar
43+
// * slug = foo-bar
44+
// Also allow for the slugified shortTitle to match the filename.
4445
slugger.reset()
45-
const slug = slugger.slug(decode(data.title))
46+
const slugTitle = slugger.slug(decode(data.title))
47+
const slugShortTitle = slugger.slug(decode(data.shortTitle))
48+
const allowedSlugs = [slugTitle, slugShortTitle]
4649

4750
// get the basename of each file
4851
// where file = content/foo-bar.md
4952
// and basename = foo-bar
5053
const basename = path.basename(oldFullPath, '.md')
5154

52-
// if slug and basename match, return early
53-
if (basename === slug) return
55+
// If the basename is one of the allowed slugs, we're all set here.
56+
if (allowedSlugs.includes(basename)) return
5457

5558
// otherwise rename the file using the slug
56-
const newFullPath = oldFullPath.replace(basename, slug)
59+
const newFullPath = oldFullPath.replace(basename, slugShortTitle || slugTitle)
5760

5861
const oldContentPath = path.relative(process.cwd(), oldFullPath)
5962
const newContentPath = path.relative(process.cwd(), newFullPath)

src/frame/tests/pages.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,16 @@ describe('pages module', () => {
8989
expect(duplicates.length, message).toBe(0)
9090
})
9191

92-
test('every English page has a filename that matches its slugified title', async () => {
92+
test('every English page has a filename that matches its slugified title or shortTitle', async () => {
9393
const nonMatches = pages
9494
.filter((page) => {
9595
slugger.reset()
9696
return (
9797
page.languageCode === 'en' && // only check English
9898
!page.relativePath.includes('index.md') && // ignore TOCs
9999
!page.allowTitleToDifferFromFilename && // ignore docs with override
100-
slugger.slug(decode(page.title)) !== path.basename(page.relativePath, '.md')
100+
slugger.slug(decode(page.title)) !== path.basename(page.relativePath, '.md') &&
101+
slugger.slug(decode(page.shortTitle || '')) !== path.basename(page.relativePath, '.md')
101102
)
102103
})
103104
// make the output easier to read

0 commit comments

Comments
 (0)