Skip to content

Commit

Permalink
Update PDF generation to use ESM modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mheap committed Jan 9, 2024
1 parent ad44e7d commit 8ff8400
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 113 deletions.
42 changes: 21 additions & 21 deletions pdf-generation/build-urls.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
const yaml = require('js-yaml')
const fs = require('fs')
import { load } from "js-yaml";
import { readFileSync } from "fs";

function extractUrls (input) {
function extractUrls(input) {
return input.flatMap((item) => {
if (item.items) {
return extractUrls(item.items)
return extractUrls(item.items);
}
return item
})
return item;
});
}

module.exports = function (input) {
let nav = yaml.load(fs.readFileSync(input.path, 'utf8'))
export default function (input) {
let nav = load(readFileSync(input.path, "utf8"));

// If it's single sourced, the nav is under nav.items
if (nav.product && nav.items) {
nav = nav.items;
}

let urls = extractUrls(nav)
const urlVersion = input.version ? `/${input.version}` : ''
let urls = extractUrls(nav);
const urlVersion = input.version ? `/${input.version}` : "";

// Build a full URL if absolute_url isn't set
urls = urls.map((url) => {
if (url.absolute_url) {
return url.url
return url.url;
}
return `/${input.type}${urlVersion}${url.url}`
})
return `/${input.type}${urlVersion}${url.url}`;
});

// Normalise URLs to remove fragments + add trailing slash
urls = urls.map((url) => {
return url.split('#')[0].replace(/\/$/, '') + '/'
})
return url.split("#")[0].replace(/\/$/, "") + "/";
});

// Unique list of URLs
urls = [...new Set(urls)]
urls = [...new Set(urls)];

// Remove any non-relative URLs
urls = urls.filter((url) => {
return !(url.includes('http://') || url.includes('https://'))
})
return !(url.includes("http://") || url.includes("https://"));
});

// Build full URLs
urls = urls.map((x) => {
return `http://localhost:8888${x}`
})
return `http://localhost:8888${x}`;
});

return urls
return urls;
}
73 changes: 38 additions & 35 deletions pdf-generation/create-pdf.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const puppeteer = require("puppeteer");
const PDFMerger = require("pdf-merger-js");
import { launch } from "puppeteer";
import PDFMerger from "pdf-merger-js";

async function createPDF(name, urls) {
const merger = new PDFMerger();
const browser = await puppeteer.launch({
headless: true,
const browser = await launch({
headless: "new",
defaultViewport: null,
});
const page = await browser.newPage();
Expand Down Expand Up @@ -42,7 +42,7 @@ async function createPDF(name, urls) {
throw new Error(
`Image failed to load: ${img.src} on ${
document.location.origin + document.location.pathname
}`
}`,
);
}
// Image hasn’t loaded yet, added an event listener to know when it does
Expand All @@ -65,7 +65,10 @@ async function createPDF(name, urls) {
document.querySelector("body").innerHTML = `
<h1>${document.querySelector("title").innerText}</h1>
<p>This content is not available in PDF format. Please see ${url.replace("http://localhost:3000", "https://docs.konghq.com")} for more details</p>
<p>This content is not available in PDF format. Please see ${url.replace(
"http://localhost:3000",
"https://docs.konghq.com",
)} for more details</p>
`;
}, url);
}
Expand All @@ -80,18 +83,18 @@ async function createPDF(name, urls) {
.navtab-content { display: block !important } /* Expand tabbed content */
.navtab-contents { padding: 0 !important; }
h4 { margin-top: 0 !important; }
`
})
`,
});

// Move header if we're on a plugin page
await page.evaluate(() => {
const header = document.querySelector('.page-header')
const header = document.querySelector(".page-header");
if (!header) {
return
return;
}
const content = document.querySelector('.page-content')
content.insertBefore(header, content.firstChild)
})
const content = document.querySelector(".page-content");
content.insertBefore(header, content.firstChild);
});

// Move page content to be in body
await page.evaluate(() => {
Expand All @@ -104,44 +107,44 @@ async function createPDF(name, urls) {

// Convert tabs to be sequential with a header in each
await page.evaluate(() => {
const nodes = document.querySelectorAll('[data-navtab-id]')
const nodes = document.querySelectorAll("[data-navtab-id]");
for (const n of nodes) {
const id = n.attributes['data-navtab-id'].value
const id = n.attributes["data-navtab-id"].value;
const content = document.querySelector(
'[data-navtab-content="' + id + '"]'
)
n.innerHTML = '<h4>' + n.innerHTML + '</h4>'
content.parentNode.insertBefore(n, content)
'[data-navtab-content="' + id + '"]',
);
n.innerHTML = "<h4>" + n.innerHTML + "</h4>";
content.parentNode.insertBefore(n, content);
}
})
});

// Remove all un-needed parts of the page
await page.evaluate(() => {
const toRemove = [
'.content-header',
'.copy-action',
'.navtab-titles'
].join(', ')
const elements = document.querySelectorAll(toRemove)
".content-header",
".copy-action",
".navtab-titles",
].join(", ");
const elements = document.querySelectorAll(toRemove);
for (let i = 0; i < elements.length; i++) {
elements[i].parentNode.removeChild(elements[i])
elements[i].parentNode.removeChild(elements[i]);
}
})
});

// Remove all links
await page.evaluate(() => {
document
.querySelectorAll('a')
.forEach((n) => n.setAttribute('href', '#/'))
})
.querySelectorAll("a")
.forEach((n) => n.setAttribute("href", "#/"));
});

// Generate the PDF
merger.add(await page.pdf({ format: 'A4', preferCSSPageSize: true }))
merger.add(await page.pdf({ format: "A4", preferCSSPageSize: true }));
}

await browser.close()
console.log(`./pdfs/${name}.pdf Created`)
await merger.save(`./pdfs/${name}.pdf`)
await browser.close();
console.log(`./pdfs/${name}.pdf Created`);
await merger.save(`./pdfs/${name}.pdf`);
}

module.exports = createPDF
export default createPDF;
23 changes: 13 additions & 10 deletions pdf-generation/list-plugins.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
const fg = require('fast-glob')
import fg from "fast-glob";

module.exports = async function (plugin, version) {
plugin = plugin || '*'
version = version || '_index'
let files = await fg(`../app/_hub/kong-inc/${plugin}/${version}.md`)
export default async function (plugin, version) {
plugin = plugin || "*";
version = version || "_index";
let files = await fg(`../app/_hub/kong-inc/${plugin}/${version}.md`);
files = files.map((f) => {
return f.replace('../app/_hub/', '/hub/').replace(/\.md$/, '.html').replace("_index", 'index')
})
return f
.replace("../app/_hub/", "/hub/")
.replace(/\.md$/, ".html")
.replace("_index", "index");
});

// Prefix with URL
files = files.map((f) => {
return `http://localhost:3000${f}`
})
return `http://localhost:3000${f}`;
});

return files
return files;
}
44 changes: 23 additions & 21 deletions pdf-generation/list-versions.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
const fg = require('fast-glob')
module.exports = async function (path) {
path = path || '*'
import fg from "fast-glob";
export default async function (path) {
path = path || "*";
const lookup = {
deck: 'deck',
kic: 'kubernetes-ingress-controller',
konnect: 'konnect',
mesh: 'mesh',
gateway: 'gateway'
}
let files = await fg(`../app/_data/docs_nav_${path}.yml`)
deck: "deck",
kic: "kubernetes-ingress-controller",
konnect: "konnect",
mesh: "mesh",
gateway: "gateway",
};
let files = await fg(`../app/_data/docs_nav_${path}.yml`);
files = files
.map((f) => {
if (f.includes('docs_nav_contributing')) {
return
if (f.includes("docs_nav_contributing")) {
return;
}
const item = { path: f }
const info = f.replace('../app/_data/docs_nav_', '').replace(/\.yml$/, '')
const item = { path: f };
const info = f
.replace("../app/_data/docs_nav_", "")
.replace(/\.yml$/, "");

const x = info.split('_')
item.type = lookup[x[0]]
item.version = x[1]
const x = info.split("_");
item.type = lookup[x[0]];
item.version = x[1];

return item
return item;
})
.filter((n) => n)
.filter((n) => n);

if (!files.length) {
throw new Error(`No matching version found for '${path}'`)
throw new Error(`No matching version found for '${path}'`);
}

return files
return files;
}
1 change: 1 addition & 0 deletions pdf-generation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "bin.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
45 changes: 19 additions & 26 deletions pdf-generation/run.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,38 @@
const path = require('path')
const serveStatic = require('serve-static')
import buildUrls from "./build-urls.js";
import createPDF from "./create-pdf.js";
import listVersions from "./list-versions.js";
import listPlugins from "./list-plugins.js";

const buildUrls = require('./build-urls')
const createPDF = require('./create-pdf')
const listVersions = require('./list-versions')
const listPlugins = require('./list-plugins')

module.exports = async function (nav) {
async function run() {
if (process.env.KONG_DOC_VERSIONS) {
return await printDocs(process.env.KONG_DOC_VERSIONS)
return await printDocs(process.env.KONG_DOC_VERSIONS);
}

if (process.env.KONG_PLUGIN_NAME) {
return await printPlugin(
process.env.KONG_PLUGIN_NAME,
process.env.KONG_PLUGIN_VERSION
)
process.env.KONG_PLUGIN_VERSION,
);
}
}

async function printDocs (nav) {
const versions = await listVersions(nav)
async function printDocs(nav) {
const versions = await listVersions(nav);

for (const v of versions) {
const title = `${v.type}-${v.version}`
const urls = buildUrls(v)
await createPDF(title, urls)
const title = `${v.type}-${v.version}`;
const urls = buildUrls(v);
await createPDF(title, urls);
}
}

async function printPlugin (plugin, version) {
const urls = await listPlugins(plugin, version)
let title = plugin
async function printPlugin(plugin, version) {
const urls = await listPlugins(plugin, version);
let title = plugin;
if (version) {
title = `${title}-${version}`
title = `${title}-${version}`;
}
await createPDF(title, urls)
await createPDF(title, urls);
}

if (require.main === module) {
module.exports().then(() => {
process.exit(0)
})
}
run();

0 comments on commit 8ff8400

Please sign in to comment.