Skip to content

Commit

Permalink
CLDR-17803 site: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
srl295 committed Sep 9, 2024
1 parent 588027f commit 2cb82b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 40 deletions.
52 changes: 32 additions & 20 deletions docs/site/assets/js/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,32 +42,44 @@ async function traverse(d, out) {
return Promise.all(promises);
}


/** replace a/b/c.md with a/b/c */
function dropmd(p) {
return p.replace(/\.md$/, "");
}


async function writeSiteMaps(out) {
// simple list of links
const links = await Promise.all(out.all.map(async ({ fullPath, title }) => {
const stat = await fs.stat(fullPath);
return {
url: dropmd(`/${fullPath}`),
lastmod: stat.mtime.toISOString(),
};
}));
const stream = new SitemapStream({ hostname: 'https://cldr.unicode.org'});
const data = (await streamToPromise(Readable.from(links).pipe(stream))).toString();
await fs.writeFile('./sitemap.xml', data, 'utf-8');
console.log('Wrote sitemap.xml');
const coll = new Intl.Collator(['und']);
const allSorted = [...out.all].sort((a, b) => coll.compare(a.fullPath, b.fullPath));
await fs.writeFile('./sitemap.md', `---\ntitle: Site Map\n---\n\n` +
allSorted.map(({fullPath, title})=>
`- [/${fullPath}](/${dropmd(fullPath)}) - ${title}`).join('\n'), 'utf-8');
console.log('Wrote sitemap.md');
const links = await Promise.all(
out.all.map(async ({ fullPath, title }) => {
const stat = await fs.stat(fullPath);
return {
url: dropmd(`/${fullPath}`),
lastmod: stat.mtime.toISOString(),
};
})
);
const stream = new SitemapStream({ hostname: "https://cldr.unicode.org" });
const data = (
await streamToPromise(Readable.from(links).pipe(stream))
).toString();
await fs.writeFile("./sitemap.xml", data, "utf-8");
console.log("Wrote sitemap.xml");
const coll = new Intl.Collator(["und"]);
const allSorted = [...out.all].sort((a, b) =>
coll.compare(a.fullPath, b.fullPath)
);
await fs.writeFile(
"./sitemap.md",
`---\ntitle: Site Map\n---\n\n` +
allSorted
.map(
({ fullPath, title }) =>
`- [/${fullPath}](/${dropmd(fullPath)}) - ${title}`
)
.join("\n"),
"utf-8"
);
console.log("Wrote sitemap.md");
}

async function main() {
Expand All @@ -78,7 +90,7 @@ async function main() {
await fs.mkdir("assets/json/", { recursive: true });
await traverse(".", out);
await fs.writeFile("assets/json/tree.json", JSON.stringify(out, null, " "));
console.log('Wrote assets/json/tree.json');
console.log("Wrote assets/json/tree.json");
await writeSiteMaps(out);
}

Expand Down
42 changes: 22 additions & 20 deletions docs/site/assets/js/cldrsite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const { ref } = Vue;

// site management

let myPath = window.location.pathname.slice(1) || "index.html"
let myPath = window.location.pathname.slice(1) || "index.html";
if (!/\.html/.test(myPath)) {
myPath = `${myPath}.html`; // cloudflare likes to drop the .html
myPath = `${myPath}.html`; // cloudflare likes to drop the .html
}

/** replace a/b/c.md with a/b */
Expand Down Expand Up @@ -143,18 +143,19 @@ const app = Vue.createApp(
if (!this.tree?.value) return [];
let dirForPage = this.ourDir;
if (this.tree.value.allIndexes.indexOf(this.mdPath) != -1) {
const dirPages = Object.entries(this.tree?.value?.allDirs)
.filter(([k, {index}]) => index == this.mdPath)[0];
if (dirPages) {
// our page is an index -so, show the subpages instead of the siblings.
dirForPage = dirPages[0]; // the adjusted index
} else {
return []; // no sibling pages;
}
const dirPages = Object.entries(this.tree?.value?.allDirs).filter(
([k, { index }]) => index == this.mdPath
)[0];
if (dirPages) {
// our page is an index -so, show the subpages instead of the siblings.
dirForPage = dirPages[0]; // the adjusted index
} else {
return []; // no sibling pages
return []; // no sibling pages;
}
let thePages = this.tree?.value?.allDirs[dirForPage].pages ?? [];
} else {
return []; // no sibling pages
}
let thePages = this.tree?.value?.allDirs[dirForPage].pages ?? [];
if (dirForPage === "") {
thePages = [...thePages, ...this.tree?.value?.allDirs["index"].pages];
}
Expand All @@ -167,35 +168,36 @@ const app = Vue.createApp(
title: this.tree.value.title[path] ?? path,
}))
.sort((a, b) => c.compare(a.title, b.title))
.filter(({html}) => html != t.path); // skip showing the index page in the subpage list
.filter(({ html }) => html != t.path); // skip showing the index page in the subpage list
},
ancestorPages() {
const pages = [];
// if we are not loaded, or if we're at the root, then exit
if (!this.tree?.value || !this.path || this.path == 'index.html') return pages;
if (!this.tree?.value || !this.path || this.path == "index.html")
return pages;
// traverse
let path = this.path;
do {
// calculate the immediate ancestor
const pathMd = html2md(path);
const dir = path2dir(path);
const nextIndex = this.tree.value.allDirs[dir].index || 'index.md'; // falls back to top
const nextIndex = this.tree.value.allDirs[dir].index || "index.md"; // falls back to top
const nextIndexHtml = md2html(nextIndex);
const nextIndexTitle = this.tree.value.title[nextIndex];
// prepend
pages.push({
href: '/'+nextIndexHtml,
href: "/" + nextIndexHtml,
title: nextIndexTitle,
});
if (nextIndexHtml == path) {
console.error('Loop detected from ' + this.path);
path = 'index.html'; // exit
console.error("Loop detected from " + this.path);
path = "index.html"; // exit
}
path = nextIndexHtml;
} while(path && path != 'index.html'); // we iterate over 'path', so html
} while (path && path != "index.html"); // we iterate over 'path', so html
pages.reverse();
return pages;
}
},
},
template: `<div>
<div class='status' v-if="status">{{ status }}</div>
Expand Down

0 comments on commit 2cb82b9

Please sign in to comment.