-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Docs: new post-processing for books so that assets stay relative
This commit has the same content as #3842
- Loading branch information
1 parent
d8bef26
commit c60b52e
Showing
8 changed files
with
1,468 additions
and
4 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
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
# this is a script called by the github CI and CD workflows to post process CSS/image/href links for serving | ||
# several mdbooks from a subdirectory | ||
|
||
cd scripts/post-process | ||
npm install | ||
node index.mjs |
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { unified } from "unified"; | ||
import parse from "rehype-parse"; | ||
import inspectUrls from "@jsdevtools/rehype-url-inspector"; | ||
import stringify from "rehype-stringify"; | ||
import { read, write } from "to-vfile"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { glob } from "glob"; | ||
|
||
async function main() { | ||
const distDir = "../../../dist/docs"; | ||
|
||
const items = []; | ||
|
||
const books = [ | ||
'developers', | ||
'docs', | ||
'operators', | ||
]; | ||
|
||
for(const book of books) | ||
{ | ||
// only process the root `index.html` files, because they have absolute paths instead of relative paths | ||
const filenames = [ path.resolve(distDir, book, 'index.html') ]; | ||
|
||
// leaving this here for a future where other files need to be processed | ||
// const filenames = await glob(path.resolve(distDir, book) + '/**/*.html'); | ||
|
||
for (const f of filenames) { | ||
// Create a Rehype processor with the inspectUrls plugin | ||
const processor = unified() | ||
.use(parse) | ||
.use(inspectUrls, { | ||
inspectEach(args) { | ||
const { url: rawUrl, propertyName } = args; | ||
const { tagName } = args.node; | ||
const filename = args.file.history[0]; | ||
|
||
const relativeFilename = path.relative(distDir, filename); | ||
const relativeDirectory = path.dirname(relativeFilename); | ||
|
||
// remove relative paths from URL | ||
const bareUrl = rawUrl.split('/').filter(c => c !== '.' && c !== '..').join('/'); | ||
let url; | ||
|
||
if(rawUrl.includes('.html#')) { | ||
url = path.join(`/${relativeDirectory}`, bareUrl); | ||
} else if(rawUrl.startsWith('#')) { | ||
url = path.join(`/`, relativeFilename + bareUrl); | ||
} else { | ||
url = path.join(`/${book}`, bareUrl); | ||
} | ||
|
||
// const item = { filename, relativeDirectory, tagName, propertyName, rawUrl, url }; | ||
const item = { tagName, rawUrl, url }; | ||
|
||
// if(tagName === 'a') { | ||
// console.log(args); | ||
// } | ||
|
||
if(!rawUrl.startsWith('http')) { | ||
if (tagName === 'link' || tagName === 'script' || tagName === 'a') { | ||
args.node.properties[propertyName] = url; | ||
items.push(item); | ||
} | ||
} | ||
} | ||
}) | ||
.use(stringify); | ||
|
||
// Read the example HTML file | ||
const filename = path.resolve(distDir, f); | ||
console.log(`${filename}...`); | ||
|
||
let file = await read(filename); | ||
|
||
// Crawl the HTML file and find all the URLs | ||
const res = await processor.process(file); | ||
|
||
fs.writeFileSync(filename, res.value); | ||
} | ||
} | ||
|
||
// console.table(items); | ||
// console.log(); | ||
} | ||
|
||
main(); |
Oops, something went wrong.