Skip to content

Commit

Permalink
chore: fix code generation page spanning
Browse files Browse the repository at this point in the history
- Stop code blocks spanning multiple pages
- Stop executed code data spanning multiple pages
  • Loading branch information
hollandjake committed Jan 8, 2025
1 parent 1c5f9db commit 2269cd0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
39 changes: 38 additions & 1 deletion docs/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ class Node {
// stores the JS so it can be run
// in the render method
this.type = 'example';
code = codeBlocks[this.attrs.alt];
code =
codeBlocks[this.attrs.alt ? this.attrs.alt : codeBlocks.length - 1];
if (code) {
this.code = code;
}
Expand Down Expand Up @@ -194,6 +195,7 @@ class Node {
if (continued == null) {
continued = false;
}
var tempDoc;
switch (this.type) {
case 'example':
this.setStyle(doc);
Expand All @@ -207,6 +209,24 @@ class Node {
({ y } = doc);
doc.x = doc.y = 0;

tempDoc = new PDFDocument({
size: [doc.page.width - x, doc.page.height - y],
margin: 0,
});
vm.runInNewContext(this.code, {
doc: tempDoc,
lorem,
});
if (y + tempDoc.y > doc.page.maxY()) doc.addPage();

// Update the page width for those which rely on the width of the document
var docPageWidth = doc.page.width;
var docPageHeight = doc.page.height;
var docPageMargins = doc.page.margins;
doc.page.width = doc.page.width - x - doc.page.margins.right;
doc.page.height = doc.page.height - y - doc.page.margins.bottom;
doc.page.margins = { top: 0, left: 0, right: 0, bottom: 0 };

// run the example code with the document
vm.runInNewContext(this.code, {
doc,
Expand All @@ -218,14 +238,31 @@ class Node {
doc.restore();
doc.x = x;
doc.y = y + this.height;
doc.page.width = docPageWidth;
doc.page.height = docPageHeight;
doc.page.margins = docPageMargins;
break;
case 'hr':
doc.addPage();
break;
case "code_block":
// Code blocks should be rendered in a single page (no page breaks)
tempDoc = new PDFDocument();
this.type = null;
this.render(tempDoc, continued);
this.type = "code_block";
if (doc.y + tempDoc.y > doc.page.maxY()) doc.addPage();
// eslint-disable-next-line no-fallthrough
default:
// loop through subnodes and render them
for (let index = 0; index < this.content.length; index++) {
const fragment = this.content[index];

if (this.type === "numberlist") {
let node = new Node(["inlinecode", `${index + 1}. `]);
fragment.content.splice(0, 0, node);
}

if (fragment.type === 'text') {
// add a new page for each heading, unless it follows another heading
if (
Expand Down
12 changes: 4 additions & 8 deletions docs/generate_website.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,13 @@ let imageIndex = 0;
const generateImages = function(tree) {
// find code blocks
const codeBlocks = [];
for (var node of tree) {
if (node[0] === 'code_block') {
for (const node of tree) {
if (node[0] === "code_block") {
codeBlocks.push(node[1]);
}
}

for (node of tree) {
if (node[0] === 'para' && Array.isArray(node[1]) && node[1][0] === 'img') {
} else if (node[0] === "para" && Array.isArray(node[1]) && node[1][0] === "img") {
// compile the code
const attrs = node[1][1];
let code = codeBlocks[attrs.alt];
let code = codeBlocks[attrs.alt ? attrs.alt : codeBlocks.length - 1];
delete attrs.height; // used for pdf generation

// create a PDF and run the example
Expand Down

0 comments on commit 2269cd0

Please sign in to comment.