Skip to content

experimental: resolve sitemap section from nav data #1544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 140 additions & 71 deletions src/components/pages/Sitemap.astro
Original file line number Diff line number Diff line change
@@ -1,90 +1,157 @@
---
import {
sitemapSectionsJa as ja,
sitemapSectionsEn as en,
type SitemapSection,
} from "@data/sitemapSections";
import en from "@data/nav/en.yml";
import ja from "@data/nav/ja.yml";
import { basename } from "path";
import type { AstroInstance, MDXInstance, MarkdownInstance } from "astro";
import type { MDXInstance, MarkdownInstance } from "astro";
import Slugger from "github-slugger";
import Layout, { Frontmatter } from "@layouts/Layout.astro";
import Layout, { type Frontmatter } from "@layouts/Layout.astro";
import type { Lang } from "@components/types";
import type { Navigation } from "@data/schemas/nav";
import Markdown from "@components/utils/Markdown.astro";
import { unified } from "unified";
import remarkParse from "remark-parse";
import { toHast } from "mdast-util-to-hast";
import { toText } from "hast-util-to-text";

interface Props {
lang: Lang;
}

interface AstroInstanceExt extends AstroInstance {
title: string;
sitemap?: boolean;
}

type PageInstance =
| MarkdownInstance<Frontmatter>
| MDXInstance<Frontmatter>
| AstroInstanceExt;
const { lang } = Astro.props;
const navigations: Navigation[] = { ja, en }[lang];

interface Page {
title: string;
url: string;
interface SectionNode {
name: string;
prefixes: string[];
children: SectionNode[];
}

interface Section extends SitemapSection {
slug: string;
links: Page[];
const parser = unified().use(remarkParse);
function text(content: string) {
const mdast = parser.parse(content);
const hast = toHast(mdast);
return toText(hast);
}

type HeadingType = "h1" | "h2" | "h3" | "h4" | "h5" | "h6";
function findChild(node: SectionNode, prefix: string): SectionNode | undefined {
if (node.prefixes.includes(prefix)) return node;
for (const child of node.children) {
const found = findChild(child, prefix);
if (found) return found;
}
}

const { lang } = Astro.props;
const root: SectionNode = {
name: "",
prefixes: [],
children: [],
};
for (const nav of navigations) {
let parent = root;
if (nav.sitemap?.section) {
parent = {
name: text(nav.name),
prefixes: [],
children: [],
};
root.children.push(parent);
}
for (const content of nav.contents) {
if (!nav.sitemap && !content.sitemap) continue;
const sitemap = { ...nav.sitemap, ...content.sitemap };

let pages = (await Astro.glob<PageInstance>("/src/pages/**/*.{astro,md,mdx}"))
.filter((page): page is PageInstance & { url: string } => {
if (!page.url) return false;
if (basename(page.url).startsWith("_")) return false;
if ("frontmatter" in page) {
const { layout, sitemap, redirect_to } = page.frontmatter;
if (layout === false || sitemap === false || redirect_to) return false;
let parentWithOverride = parent;
if (sitemap.parent) {
const overridingParent = findChild(root, sitemap.parent);
if (!overridingParent) {
throw new Error(`Parent not found: ${sitemap.parent}`);
}
parentWithOverride = overridingParent;
}
if (sitemap.section) {
const section = {
name: text(content.name),
prefixes: [content.url],
children: [],
};
parentWithOverride.children.push(section);
} else {
const { sitemap } = page;
if (sitemap === false) return false;
parentWithOverride.prefixes.push(content.url);
}
return page.url.startsWith("/en/") === (lang === "en");
})
.map<Page>((page) => {
const { title } = "frontmatter" in page ? page.frontmatter : page;
return { title, url: page.url };
});
}
}
root.children.push({
name: { ja: "その他", en: "Others" }[lang],
prefixes: [{ ja: "/", en: "/en/" }[lang]],
children: [],
});

type Depth = 1 | 2 | 3 | 4 | 5 | 6;
function nextDepth(depth: Depth) {
if (depth >= 6) throw new Error("Depth too deep");
return (depth + 1) as Depth;
}
interface Entry {
title: string;
url: string;
}
interface Section {
name: string;
slug: string;
depth: Depth;
prefixes: string[];
entries: Entry[];
}

// https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values#comment48111034_979289
// @ts-ignore
pages.sort((a, b) => (a.url > b.url) - (a.url < b.url));
function resolveSections(
node: SectionNode,
slugger = new Slugger(),
depth: Depth = 1,
) {
const sections: Section[] = [
{
name: node.name,
slug: slugger.slug(node.name),
depth,
prefixes: node.prefixes,
entries: [],
},
];
for (const child of node.children) {
const childSections = resolveSections(child, slugger, nextDepth(depth));
sections.push(...childSections);
}
return sections;
}
const sections = resolveSections(root);
sections.shift(); // remove root

const slugger = new Slugger();
type Page = MarkdownInstance<Frontmatter> | MDXInstance<Frontmatter>;
const pages = await Astro.glob<Page>("/src/pages/**/*.{md,mdx}");

const sections = { ja, en }[lang].map<Section>((section) => {
let links: Page[] = [];
section.patterns?.forEach((pattern) => {
pages = pages.filter((page) => {
let cond = false;
cond ||= pattern.test(page.url);
if (section.negativePatterns) {
cond &&= !section.negativePatterns.some((pattern) =>
pattern.test(page.url)
);
}
if (cond) {
links.push(page);
}
return !cond;
});
});
return {
...section,
slug: slugger.slug(section.name),
links,
};
});
const indexSectionByPrefixes = sections.flatMap((section) =>
section.prefixes.map((prefix) => ({ prefix, section })),
);
// longest prefix first
indexSectionByPrefixes.sort((a, b) => b.prefix.length - a.prefix.length);
for (const page of pages) {
if (!page.url) continue;
if (basename(page.url).startsWith("_")) continue;
const { layout, sitemap, redirect_to } = page.frontmatter;
if (layout === false || sitemap === false || redirect_to) continue;
if (page.url.startsWith("/en/") !== (lang === "en")) continue;

const { section } = indexSectionByPrefixes.find(
(item) =>
page.url!.startsWith(item.prefix) || page.url + "/" === item.prefix,
)!; // the "Others" section should catch
section.entries.push({ title: page.frontmatter.title, url: page.url });
}
for (const section of sections) {
// https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values#comment48111034_979289
// @ts-ignore
section.entries.sort((a, b) => (a.url > b.url) - (a.url < b.url));
}

const meta = {
file: `src/pages${{ en: "/en", ja: "" }[lang]}/sitemap.astro`,
Expand All @@ -102,23 +169,25 @@ const meta = {
...meta,
}}
headings={sections.map((section) => ({
text: section.name,
text: text(section.name),
slug: section.slug,
depth: section.depth,
}))}
{...meta}
>
{
sections.map((section) => {
const Heading: HeadingType = `h${section.depth}`;
const Heading = `h${section.depth}` as const;
return (
<>
<Heading id={section.slug}>{section.name}</Heading>
<Heading id={section.slug}>
<Markdown content={section.name} />
</Heading>
<ul>
{section.links.map((link) => {
{section.entries.map(({ url, title }) => {
return (
<li>
<a href={link.url}>{link.title}</a>
<a href={url}>{title}</a>
</li>
);
})}
Expand Down
23 changes: 23 additions & 0 deletions src/data/nav/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
-
name: "For New Students"
url: "/en/oc/"
sitemap:
section: true
-
name: "For Faculty Members"
url: "/en/faculty_members/"
sitemap:
section: true
-
name: "For Staff Members"
url: "/en/staff_members/"
Expand All @@ -20,6 +24,8 @@
url: "/en/support/"
-
name: "ICT Systems at UTokyo"
sitemap:
section: true
contents:
-
name: "UTokyo Account"
Expand Down Expand Up @@ -57,9 +63,13 @@
-
name: "UTokyo Azure"
url: "/en/research_computing/utokyo_azure/"
sitemap:
parent: "/en/research_computing/"
-
name: "Full List"
url: "/en/systems/"
sitemap:
section: false
# -
# name: "List of all systems"
# url: "/en/systems/"
Expand All @@ -69,6 +79,8 @@
-
name: "Effective Use of Online Resources"
url: "/en/online/"
sitemap:
section: true
-
name: "Search Online Resources by Tool"
url: "/en/online/tools"
Expand All @@ -78,12 +90,21 @@
-
name: "Copyright Handling on Creating Materials"
url: "/en/articles/copyright/"
-
name: "Articles"
url: "/en/articles/"
hidden: true
sitemap:
section: false
parent: "/en/online/"
-
name: "Guides / Events"
contents:
-
name: "Notice"
url: "/en/notice/"
sitemap:
section: true
-
name: "Information Security Portal Site"
url: "https://univtokyo.sharepoint.com/sites/Security/SitePages/en/Home.aspx"
Expand All @@ -99,6 +120,8 @@
-
name: "Orientations / Seminars"
url: "/en/events/"
sitemap:
section: true
# -
# name: "Community of Online Education Practices"
# url: "/en/events/2020-luncheon/"
Expand Down
Loading