Skip to content

Commit 4df41fc

Browse files
authored
Fix template generation for WWW + Community (#84)
1 parent 30d2e81 commit 4df41fc

File tree

4 files changed

+20
-141
lines changed

4 files changed

+20
-141
lines changed

package-lock.json

Lines changed: 0 additions & 127 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"html-webpack-plugin": "^5.6.0",
4141
"jsdom": "^23.2.0",
4242
"mini-css-extract-plugin": "^2.7.7",
43-
"node-fetch": "^3.3.2",
4443
"path-browserify": "^1.0.1",
4544
"posthtml": "^0.16.6",
4645
"posthtml-expressions": "^1.11.3",

src/build/createGitHubComment.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 DigitalOcean
2+
Copyright 2024 DigitalOcean
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
const fetch = import('node-fetch');
18-
1917
module.exports = async args => {
2018
const baseURL = `https://${process.env.SPACES_BUCKET}.${process.env.SPACES_REGION}.digitaloceanspaces.com`;
2119
const tools = args.map(data => {
@@ -28,7 +26,7 @@ module.exports = async args => {
2826
| ${tools.join(' | ')} |
2927
|${(new Array(tools.length).fill('---')).join('|')}|`;
3028

31-
const res = await fetch.then(({ default: run }) => run(
29+
const res = await fetch(
3230
`https://api.github.com/repos/${process.env.REPO_NAME}/commits/${process.env.COMMIT_SHA}/comments`,
3331
{
3432
method: 'POST',
@@ -41,7 +39,7 @@ module.exports = async args => {
4139
Authorization: `Basic ${Buffer.from(`github-actions:${process.env.GITHUB_ACCESS_TOKEN}`).toString('base64')}`,
4240
},
4341
},
44-
)).catch(async e => {
42+
).catch(async e => {
4543
console.log(await e.json());
4644
process.exit(1);
4745
});

src/build/fetchTemplate.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const { JSDOM } = jsdom;
1919
const fs = require('fs');
2020
const ensureDir = require('./ensureDir');
2121

22-
const fetch = import('node-fetch');
22+
const wwwSelectors = {
23+
content: '[class^="Layout"] > *, [class*=" Layout"] > *',
24+
header: 'nav, [class^="Header"], [class*=" Header"]',
25+
footer: 'footer, [class^="Footer"], [class*=" Footer"], #footer',
26+
};
2327

2428
const baseHtml = async mode => {
2529
// Support a blank template for completely local dev
@@ -38,13 +42,13 @@ const baseHtml = async mode => {
3842

3943
// Support developing a tool for WWW
4044
if (mode === 'www') {
41-
const res = await fetch.then(({ default: run }) => run('https://www.digitalocean.com'));
45+
const res = await fetch('https://www.digitalocean.com');
4246
return await res.text();
4347
}
4448

4549
// Default to a tool for Community tooling
4650
if (mode === 'community') {
47-
const res = await fetch.then(({ default: run }) => run('https://www.digitalocean.com/community'));
51+
const res = await fetch('https://www.digitalocean.com/community');
4852
return await res.text();
4953
}
5054

@@ -105,17 +109,22 @@ module.exports = async () => {
105109
document.head.insertBefore(charset, document.head.firstChild);
106110
}
107111

112+
let contentAnchor;
108113
if (mode === 'www' || mode === 'community') {
109114
// Remove nav log in + sign up buttons
110-
document.querySelectorAll('nav li').forEach(node => {
115+
document.querySelectorAll('header li').forEach(node => {
111116
if (node.textContent.toLowerCase().includes('log in')) node.remove();
112117
if (node.textContent.toLowerCase().includes('sign up')) node.remove();
113118
});
114119

115120
// Remove www + community content
116-
document.querySelectorAll('div[class^="Layout"] > *').forEach(node => {
117-
if (node.querySelector('nav, [class^="Header"], [class*=" Header"]')) return;
118-
if (node.querySelector('footer, [class^="Footer"], [class*=" Footer"]')) return;
121+
document.querySelectorAll(wwwSelectors.content).forEach(node => {
122+
if (node.querySelector(wwwSelectors.header)) return;
123+
if (node.querySelector(wwwSelectors.footer)) return;
124+
125+
// If this is the first content node on the page,
126+
// we'll inject the content block where it is
127+
if (!contentAnchor) contentAnchor = node.previousElementSibling;
119128
node.remove();
120129
});
121130
}
@@ -127,7 +136,7 @@ module.exports = async () => {
127136
document.querySelector('body > .app').appendChild(content);
128137
}
129138
if (mode === 'www' || mode === 'community') {
130-
document.querySelector('nav').parentElement.insertAdjacentElement('afterend', content);
139+
contentAnchor.insertAdjacentElement('afterend', content);
131140
}
132141

133142
// Inject the title block

0 commit comments

Comments
 (0)