diff --git a/CHANGELOG.md b/CHANGELOG.md index af02805..07af69b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Docs0 Changelog +## 1.1.2 (2024-10-06) +- Improve PDF generation + ## 1.1.1 (2024-10-04) - Theme fixes - Support checkboxes in markdown diff --git a/_includes/pdf.html b/_includes/pdf.html index 8b501c0..e4da2b3 100644 --- a/_includes/pdf.html +++ b/_includes/pdf.html @@ -14,40 +14,61 @@ pageUrl.search = ""; const contentElement = document.querySelector(".page-content"); - + // Convert relative links to absolute URLs contentElement.querySelectorAll("a").forEach(function(link) { - if (link.getAttribute("href") && !link.getAttribute("href").startsWith("http")) { - const absoluteURL = new URL(link.getAttribute("href"), window.location.origin).href; - link.setAttribute("href", absoluteURL); - } + if (link.getAttribute("href") && !link.getAttribute("href").startsWith("http")) { + const absoluteURL = new URL(link.getAttribute("href"), window.location.origin).href; + link.setAttribute("href", absoluteURL); + } }); - const docDefinition = { - content: htmlToPdfmake(` -

{{ page.title }}

- ${contentElement.innerHTML} - {% if site.data.commit_dates[page.path] or page.modified or page.date %} -

- Last update: - {% if site.data.commit_dates[page.path] %} - {{ site.data.commit_dates[page.path] | date: "%b %d, %Y" }} - {% elsif page.modified %} - {{ page.modified | date: "%b %d, %Y" }} - {% else %} - {{ page.date | date: "%b %d, %Y" }} - {% endif %} -

- {% endif %} - Source: ${pageUrl.toString()} - `, { - defaultStyles: { - h1: { fontSize:30, bold:true, lineHeight:1.1, marginBottom:10 }, + const content = htmlToPdfmake(` +

{{ page.title }}

+ ${contentElement.innerHTML} + {% if site.data.commit_dates[page.path] or page.modified or page.date %} +

+ Last update: + {% if site.data.commit_dates[page.path] %} + {{ site.data.commit_dates[page.path] | date: "%b %d, %Y" }} + {% elsif page.modified %} + {{ page.modified | date: "%b %d, %Y" }} + {% else %} + {{ page.date | date: "%b %d, %Y" }} + {% endif %} +

+ {% endif %} +

Source: ${pageUrl.toString()}

+ `, { + defaultStyles: { + h1: { fontSize:30, bold:true, lineHeight:1.1, marginBottom:10 }, + h2: { marginTop:30 }, + h3: { marginTop:30 }, + h4: { marginTop:20 }, + h5: { marginTop:10 }, + h6: { marginTop:10 }, + }, + //removeExtraBlanks: true, + }); + + const removeBlank = node => { + if (node.text && typeof node.text === "string" && node.text.trim() === "") + return false; + const props = ["stack", "ul", "ol"]; + for (const prop of props) { + if (node[prop]) { + node[prop] = node[prop].filter(removeBlank); + if (node[prop].length === 0) return false; } - }), + } + return true; + }; + + const docDefinition = { + content: content.filter(removeBlank), styles: { source: { - fontSize: 10, + fontSize: 10 } }, defaultStyle: { diff --git a/package.json b/package.json index aadef9e..47e8890 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "Docs0", - "version": "1.1.1", - "lastUpdated": "2024-10-04", + "version": "1.1.2", + "lastUpdated": "2024-10-06", "scripts": { "run (incremental)": "bundle exec jekyll serve --incremental --port 4001", "run (full)": "bundle exec jekyll serve --port 4001",