Repository: Mogztter/asciidoctor-pdf.js
Pull request #62 (2019-09-03 13:13:29)

Include a custom stylesheet
ref #60 


Patch:
diff --git a/lib/document/templates.js b/lib/document/templates.js
index 1ae97ce..828586c 100644
--- a/lib/document/templates.js
+++ b/lib/document/templates.js
@@ -1,4 +1,5 @@
 const fs = require('fs')
+const path = require('path')
 
 const repeatTableHeadersContent = fs.readFileSync(`${__dirname}/repeating-table-headers.js`, 'utf8')
 const pagedContent = fs.readFileSync(require.resolve('pagedjs/dist/paged.polyfill.js'), 'utf8')
@@ -7,6 +8,36 @@ const documentStyleContent = fs.readFileSync(`${__dirname}/document.css`, 'utf8'
 const titleDocumentStyleContent = fs.readFileSync(`${__dirname}/title-document-numbering.css`, 'utf8')
 const titlePageStyleContent = fs.readFileSync(`${__dirname}/title-page-numbering.css`, 'utf8')
 
+const customStyleContent = (node) => {
+  const stylesheet = node.getAttribute('stylesheet')
+  if (stylesheet && stylesheet !== '') {
+    if (path.isAbsolute(stylesheet)) {
+      return fs.readFileSync(stylesheet)
+    }
+    const stylesDirectory = node.getAttribute('stylesdir')
+    let start = node.getDocument().getBaseDir()
+    if (stylesDirectory) {
+      if (path.isAbsolute(stylesDirectory)) {
+        start = stylesDirectory
+      } else {
+        start = path.join(node.getDocument().getBaseDir(), stylesDirectory)
+      }
+    } else {
+      start = node.getDocument().getBaseDir()
+    }
+    return fs.readFileSync(path.join(start, stylesheet))
+  }
+  return ''
+}
+
+const titlePageStyle = (node) => {
+  if (hasTitlePage(node)) {
+    // The page number start after the first page (ie. the title page)
+    return titlePageStyleContent
+  }
+  return titleDocumentStyleContent
+}
+
 const hasTitlePage = (node) => {
   const doc = node.getDocument()
   return doc.getDoctype() === 'book' || doc.hasAttribute('title-page')
@@ -62,14 +93,6 @@ const titlePage = (node) => {
   return ''
 }
 
-const titlePageStyle = (node) => {
-  if (hasTitlePage(node)) {
-    // The page number start after the first page (ie. the title page)
-    return titlePageStyleContent
-  }
-  return titleDocumentStyleContent
-}
-
 module.exports = {
   document: (node) => {
     const cdnBaseUrl = `${assetUriScheme(node)}//cdnjs.cloudflare.com/ajax/libs`
@@ -86,6 +109,7 @@ ${node.getContent()}
 ${asciidoctorStyleContent}
 ${documentStyleContent}
 ${titlePageStyle(node)}
+${customStyleContent(node)}
 </style>
 <script>
 ${pagedContent}


Test patch:
diff --git a/test/fixtures/custom.css b/test/fixtures/custom.css
new file mode 100644
index 0000000..68333ae
--- /dev/null
+++ b/test/fixtures/custom.css
@@ -0,0 +1,1 @@
+.greetings{color: #fecbcb;}
diff --git a/test/templates_test.js b/test/templates_test.js
index 89a6835..6e3e318 100644
--- a/test/templates_test.js
+++ b/test/templates_test.js
@@ -48,4 +48,10 @@ Guillaume Grossetie
     const $ = cheerio.load(templates.document(doc))
     expect($('.title-document > h1').length).to.equal(0)
   })
+
+  it('should include a custom stylesheet', () => {
+    const doc = asciidoctor.load('[.greetings]#Hello world#', { attributes: { stylesheet: `${__dirname}/fixtures/custom.css` } })
+    const $ = cheerio.load(templates.document(doc))
+    expect($('head > style').html()).to.have.string('.greetings{color: #fecbcb;}')
+  })
 })