Skip to content

Commit d5e7c38

Browse files
committed
Fix a bug when adding a script or style from both a page and its template
1 parent 5e0d7a3 commit d5e7c38

File tree

3 files changed

+10
-25
lines changed

3 files changed

+10
-25
lines changed

system/.prettierrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

system/compile-page.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,15 @@ function compileHtml(page: Page, pages: Page[]) {
6868
let html = template.body
6969

7070
// Add extra CSS or scripts
71-
frontmatter.styles ??= frontmatter.style ?? ""
72-
frontmatter.styles += template.frontmatter.styles ?? template.frontmatter.style ?? ""
73-
if (frontmatter.styles) {
74-
for (const style of trimAll(frontmatter.styles.split(","))) {
75-
const styleTag = `<link rel="stylesheet" href="${style}">`
76-
html = html.replace("</head>", `${indent(styleTag)}\n</head>`)
77-
}
78-
}
79-
frontmatter.scripts ??= frontmatter.script ?? ""
80-
frontmatter.scripts += template.frontmatter.scripts ?? template.frontmatter.script ?? ""
81-
if (frontmatter.scripts) {
82-
for (const script of trimAll(frontmatter.scripts.split(","))) {
83-
const scriptTag = `<script defer src="${script}"></script>`
84-
html = html.replace("</body>", `${indent(scriptTag)}\n</body>`)
85-
}
86-
}
71+
// We accept both the singular and the plural, from both the page and the template
72+
let styles = [frontmatter.styles, frontmatter.style, template.frontmatter.styles, template.frontmatter.style]
73+
let scripts = [frontmatter.scripts, frontmatter.script, template.frontmatter.scripts, template.frontmatter.script]
74+
// combine and then filter empty values
75+
styles = trimAll(styles.join(",").split(",")).filter((v) => v)
76+
scripts = trimAll(scripts.join(",").split(",")).filter((v) => v)
77+
// insert at the end of the head
78+
for (const style of styles) html = html.replace("</head>", ` <link rel="stylesheet" href="${style}">\n</head>`)
79+
for (const script of scripts) html = html.replace("</head>", ` <script defer src="${script}"></script>\n</head>`)
8780

8881
html = expandMacros(html, page, pages)
8982

system/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const unique = <T>(arr: T[]) => Array.from(new Set(arr))
1111
type Existing<T> = T extends null | undefined | void ? never : T
1212
export const compact = <T>(arr: (T | null)[]): Existing<T>[] => arr.filter(nonnull)
1313
export const nonnull = <T>(v: T): v is Existing<T> => v != null
14-
export const indent = (str: string, spaces = " ") => splitLines(str).map((line) => spaces + line).join("\n") // prettier-ignore
14+
export const indent = (str = "", spaces = " ") => splitLines(str).map((line) => spaces + line).join("\n") // prettier-ignore
1515
export const splitOnce = (str: string, sep: string) => {
1616
const i = str.indexOf(sep)
1717
return i === -1 ? [str] : [str.slice(0, i), str.slice(i + sep.length)]

0 commit comments

Comments
 (0)