Skip to content

Commit

Permalink
Link non-post pages in root by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredkrinke committed Dec 9, 2021
1 parent 57147c4 commit aa6870d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
19 changes: 13 additions & 6 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ function replaceLink(link: string) {
return link.replace(/^([^/][^:]*)\.md(#[^#]+)?$/, "$1.html$2")
}

function capitalize(str: string): string {
if (str.length > 0) {
return str[0].toLocaleUpperCase() + str.substring(1);
}
return str;
}

const noop: GoldsmithPlugin = (_files, _goldsmith) => {};

await Goldsmith()
Expand Down Expand Up @@ -201,15 +208,15 @@ await Goldsmith()
const text = site.header?.text ?? site.description;
let links = site.header?.links;
if (!links) {
links = site.header?.links ?? {
"home": "index.html",
"archive": "posts/index.html",
};

links = {};
for (const file of metadata.collections!.nonPosts) {
const pathFromRoot = file.pathFromRoot!;
if (pathFromRoot !== "index.html") {
const name = pathFromRoot.replace(/\.[^.]*$/, "");
const name = capitalize(
pathFromRoot
.replace(/\.[^.]*$/, "")
.replace("-", " ")
);
links[name] = pathFromRoot;
}
}
Expand Down
4 changes: 2 additions & 2 deletions schema/site.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@
}
},
"header": {
"description": "Optional subtitle and top-level links (added to all pages). By default, the site description is used as the subtitle and links to the home page, archive, and non-post pages in the site root are shown.",
"description": "Optional subtitle and top-level links (added to all pages). By default, the site description is used as the subtitle and links to non-post pages in the site root are shown.",
"type": "object",
"properties": {
"text": {
"description": "Optional subtitle (added to all pages). By default, the site description is used as the subtitle.",
"type": "string"
},
"links": {
"description": "Optional map of top-level link names to relative paths or URLs. Use `index.html` to link to the home page and `posts/index.html` to link to the archive. By default, links to the home page, archive, and all non-post pages in the site root are shown.",
"description": "Optional map of top-level link names to relative paths or URLs. Use `index.html` to link to the home page and `posts/index.html` to link to the archive. By default, links to non-post pages in the site root are shown.",
"type": "object",
"additionalProperties": {
"type": "string"
Expand Down
4 changes: 2 additions & 2 deletions schema/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export interface SiteMetadata {
/** Color used only for syntax highlighting (generally for comments). */
comment?: HexColor;
};
/** Optional subtitle and top-level links (added to all pages). By default, the site description is used as the subtitle and links to the home page, archive, and non-post pages in the site root are shown. */
/** Optional subtitle and top-level links (added to all pages). By default, the site description is used as the subtitle and links to non-post pages in the site root are shown. */
header?: {
/** Optional subtitle (added to all pages). By default, the site description is used as the subtitle. */
text?: string;
/** Optional map of top-level link names to relative paths or URLs. Use `index.html` to link to the home page and `posts/index.html` to link to the archive. By default, links to the home page, archive, and all non-post pages in the site root are shown. */
/** Optional map of top-level link names to relative paths or URLs. Use `index.html` to link to the home page and `posts/index.html` to link to the archive. By default, links to non-post pages in the site root are shown. */
links?: {
[key: string]: string;
};
Expand Down
4 changes: 2 additions & 2 deletions templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ ${{verbatim: o?.headVerbatim ?? ""}}
<header>
<h1><a href="${m.pathToRoot!}index.html">${m.site!.title!}</a></h1>
${{verbatim: m.site?.header?.text ? html`<p>${m.site.header.text}</p>` : ""}}
${{verbatim: m.site?.header?.links ? html`<nav class="main"><ul>
${{verbatim: (m.site?.header?.links && Object.keys(m.site.header.links).length > 0) ? html`<nav class="site"><ul>
${{verbatim: Object.entries(m.site.header.links).map(([name, link]) => html`<li><a href="${m.pathToRoot!}${link}">${name}</a></li>`).join("\n")}}
</ul></nav>` : ""}}
${{verbatim: o?.navigationVerbatim ?? ""}}
Expand All @@ -276,7 +276,7 @@ interface PartialNavigationOptions {
}

function partialNavigation(m: GoldsmithLiteralHTMLLayoutContext, tags: string[], o?: PartialNavigationOptions): string {
return tags ? html`<nav>
return (tags && tags.length > 0) ? html`<nav>
<strong>Topics:&nbsp;</strong>
<ul>
${{verbatim: tags.map(t => (o?.isTagIndex && t === o.tag) ? html`<li>${o.tag}</li>` : html`<li><a href="${m.pathToRoot ?? ""}posts/${t}/index.html">${t}</a></li>`).join("\n")}}
Expand Down

0 comments on commit aa6870d

Please sign in to comment.