Skip to content

Commit 44dec4b

Browse files
committed
improve auto-capitalization for title generation
1 parent ce732fb commit 44dec4b

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/core/text.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,49 @@ export {
2020
normalizeNewlines,
2121
} from "./lib/text.ts";
2222

23-
export function capitalize(str: string) {
23+
export function capitalizeWord(str: string) {
2424
return str.slice(0, 1).toUpperCase() + str.slice(1);
2525
}
2626

27+
export function capitalizeTitle(str: string) {
28+
return str.split(/\s+/).map((str, index, arr) => {
29+
if (
30+
index === 0 || index === (arr.length - 1) || !isNotCapitalized(str)
31+
) {
32+
return capitalizeWord(str);
33+
} else {
34+
return str;
35+
}
36+
}).join(" ");
37+
}
38+
39+
function isNotCapitalized(str: string) {
40+
return [
41+
// articles
42+
"a",
43+
"an",
44+
"the",
45+
// coordinating conjunctions
46+
"for",
47+
"and",
48+
"nor",
49+
"but",
50+
"or",
51+
"yet",
52+
"so",
53+
// prepositions
54+
"with",
55+
"at",
56+
"by",
57+
"to",
58+
"in",
59+
"for",
60+
"from",
61+
"of",
62+
"on",
63+
].includes(str);
64+
}
65+
2766
export function formatLineRange(
2867
text: string,
2968
firstLine: number,

src/project/types/website/website-sidebar-auto.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
pathWithForwardSlashes,
1717
safeExistsSync,
1818
} from "../../../core/path.ts";
19+
import { capitalizeTitle } from "../../../core/text.ts";
1920
import { engineValidExtensions } from "../../../execute/engine.ts";
2021
import { inputTargetIndex, resolveInputTarget } from "../../project-index.ts";
2122

@@ -151,7 +152,7 @@ async function nodesToEntries(
151152
});
152153
} else {
153154
entries.push({
154-
title: basename(href),
155+
title: titleFromPath(href),
155156
children: await nodesToEntries(
156157
project,
157158
`${root}${node}/`,
@@ -182,19 +183,28 @@ async function nodesToEntries(
182183
}
183184
});
184185
}
185-
186186
async function entryFromHref(project: ProjectContext, href: string) {
187187
const index = await inputTargetIndex(project, href);
188188
return {
189189
title: index?.title ||
190190
(await resolveInputTarget(project, href))?.title ||
191-
dirAndStem(href)[1],
191+
titleFromPath(dirAndStem(href)[1]),
192192
href: href,
193193
order: asNumber(index?.markdown.yaml?.[kOrder]),
194194
empty: index?.markdown.markdown.trim().length == 0,
195195
};
196196
}
197197

198+
function titleFromPath(path: string) {
199+
const name = basename(path);
200+
// if there are no spaces then try to split on dashes/underscoes and autocapitalize
201+
if (!name.includes(" ")) {
202+
return capitalizeTitle(name.replaceAll(/[_\-]+/g, " "));
203+
} else {
204+
return name;
205+
}
206+
}
207+
198208
function sidebarItemsFromEntries(entries: Entry[]): SidebarItem[] {
199209
return entries.map((entry) => {
200210
if (entry.children) {

src/publish/common/publish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import { fileProgress } from "../../core/progress.ts";
2424

2525
import { PublishRecord } from "../types.ts";
2626
import { PublishFiles } from "../provider.ts";
27-
import { capitalize } from "../../core/text.ts";
2827
import { gfmAutoIdentifier } from "../../core/pandoc/pandoc-id.ts";
2928
import { randomHex } from "../../core/random.ts";
3029
import { copyTo } from "../../core/copy.ts";
@@ -33,6 +32,7 @@ import { globalTempContext } from "../../core/temp.ts";
3332
import { formatResourcePath } from "../../core/resources.ts";
3433
import { encodeAttributeValue } from "../../core/html.ts";
3534
import { haveArrowKeys } from "../../core/platform.ts";
35+
import { capitalizeWord } from "../../core/text.ts";
3636

3737
export interface PublishSite {
3838
id?: string;
@@ -421,5 +421,5 @@ async function promptForSlug(
421421
}
422422

423423
function typeName(type: string) {
424-
return capitalize(type);
424+
return capitalizeWord(type);
425425
}

0 commit comments

Comments
 (0)