Skip to content

Commit

Permalink
fix(deps): update dependency pdf-merger-js to v5 (#6707)
Browse files Browse the repository at this point in the history
* Update dependency pdf-merger-js to v5
* Update PDF generation to use ESM modules
* Fix PDF build workflow

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Michael Heap <[email protected]>
  • Loading branch information
renovate[bot] and mheap committed Jan 10, 2024
1 parent c8cf081 commit 9b4a7bb
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 122 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/build-docs-pdf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
uses: ruby/setup-ruby@v1
with:
ruby-version: .ruby-version
- name: Install foreman
run: gem install foreman
- name: Ruby gem cache
uses: actions/cache@v3
with:
Expand All @@ -41,7 +43,7 @@ jobs:
# Configure Node to build assets
- uses: actions/setup-node@v4
with:
node-version: "16"
node-version: "20"
- name: Cache node modules
uses: actions/cache@v3
env:
Expand Down
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;
}
17 changes: 10 additions & 7 deletions pdf-generation/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion 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 All @@ -12,7 +13,7 @@
"dependencies": {
"fast-glob": "^3.2.5",
"js-yaml": "^4.1.0",
"pdf-merger-js": "^4.0.0",
"pdf-merger-js": "^5.0.0",
"puppeteer": "^21.0.0",
"serve-static": "^1.14.1"
}
Expand Down
Loading

0 comments on commit 9b4a7bb

Please sign in to comment.