-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update PDF generation to use ESM modules
- Loading branch information
Showing
6 changed files
with
115 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |