Skip to content
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

feat(macros): create a PWA sidebar #8238

Merged
merged 11 commits into from
Apr 5, 2023
117 changes: 117 additions & 0 deletions kumascript/macros/PWASidebar.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<%

const sections = [
{
name: "Guides",
contents: [
"Guides/What_is_a_PWA",
caugner marked this conversation as resolved.
Show resolved Hide resolved
"Guides/Benefits_of_PWAs",
{
name: "Guides/Main_features_of_PWAs",
contents: [
"Guides/Main_features_of_PWAs/Installability",
"Guides/Main_features_of_PWAs/Offline_operation",
],
},
],
},
{
name: "Tutorials",
contents: [
{
name: "Tutorials/Your_first_PWA",
contents: [
"Tutorials/Your_first_PWA/First_step",
"Tutorials/Your_first_PWA/Next_step",
],
},
{
name: "Tutorials/Your_second_PWA",
contents: [
"Tutorials/Your_second_PWA/First_step",
"Tutorials/Your_second_PWA/Next_step",
],
},
],
},
{
name: "How-to",
contents: [
"How-to/Handle_app_updates",
"How-to/Implement_a_custom_install_flow",
],
},
{
name: "Reference",
contents: [],
},
];

const sidebarURL = `/docs/Web/Progressive_web_apps/`;
const baseURL = `/${env.locale}${sidebarURL}`;

async function getTitle(pageSlug) {
let page = await wiki.getPage(`${baseURL}${pageSlug}`);
if (!page.title) {
page = await wiki.getPage(`/en-US${sidebarURL}${pageSlug}`);
}
return page.title;
}

async function renderSubsection(subsection) {
let output = `<li><details><summary>${await getTitle(
subsection.name
)}</summary><ol>`;

for (const item of subsection.contents) {
output += await renderItem(item);
}
wbamberg marked this conversation as resolved.
Show resolved Hide resolved
output += "</ol></details></li>";

return output;
}

async function renderItem(item) {
let output = "";
if (typeof item === "string") {
output += `<li><a href="${baseURL}${item}">${await getTitle(
item
)}</a></li>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably makes sense to extract renderLink(), which determines both the href and the text.

If the page does not exist in the current locale, I think we'll want to add (en-US) behind, like web.smartlink() already does (I don't mind not using web.smartlink() for now, but we should extract all these helper methods somewhere before replicating them to another sidebar anyways).

} else {
output += `<li><ol>${await renderSubsection(item)}</ol></li>`;
}
return output;
}

async function renderSection(section) {
let output = `<li><a href="${baseURL}${
section.name
}"><strong>${await getTitle(section.name)}</strong></a></li>`;

for (const item of section.contents) {
output += await renderItem(item);
}

return output;
}

async function renderSidebar() {
let output = '<section id="Quick_links"><ol>';
wbamberg marked this conversation as resolved.
Show resolved Hide resolved

output += `<li><a href="${baseURL}"><strong>${await getTitle(
""
)}</strong></a></li>`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract this as renderRootItem() to describe what this does?


for (const section of sections) {
output += await renderSection(section);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do the same as in line 59:

Suggested change
for (const section of sections) {
output += await renderSection(section);
}
output += (await Promise.all(sections.map(section => renderSection(section)))).join('');

output += "</ol></section>";

return output;
}

const output = await renderSidebar();

%>

<%-output%>