asciidoctor-prism-extension
Advanced tools
Comparing version 1.0.1 to 2.0.1
84
index.js
@@ -39,48 +39,56 @@ 'use strict'; | ||
module.exports = function prismExtension () { | ||
this.treeProcessor(function(){ | ||
this.process(doc => { | ||
if (doc.backend !== 'html5') { | ||
return doc; | ||
} | ||
module.exports = { | ||
initialize (name, backend, {$$smap: {document}}) { | ||
const languages = getDocumentLanguages(document); | ||
const languages = getDocumentLanguages(doc); | ||
loadLanguages(languages); | ||
loadLanguages(languages); | ||
this.backend = backend; | ||
this.theme = document.getAttribute('prism-theme'); | ||
this.languages = languages; | ||
doc.findBy({ context: 'listing' }, hasLanguage).forEach(block => { | ||
const lang = block.getAttribute('language'); | ||
this.super(); | ||
}, | ||
if (Prism.languages[lang] === undefined) { | ||
throw TypeError(`Prism language ${lang} is not loaded (loaded: ${languages}).`); | ||
} | ||
format (node, lang) { | ||
node.removeSubstitution('specialcharacters'); | ||
node.removeSubstitution('specialchars'); | ||
const output = Prism.highlight(unescape(block.getContent()), Prism.languages[lang]); | ||
block.lines = output.replace(/____(\d+)____/gi, '<b class="conum">($1)</b>').split('\n'); | ||
block.removeSubstitution('specialcharacters'); | ||
block.removeSubstitution('specialchars'); | ||
block.addRole('prismjs'); | ||
block.addRole('highlight-prismjs'); | ||
}); | ||
}); | ||
}); | ||
if (Prism.languages[lang] === undefined) { | ||
const {languages} = this; | ||
throw TypeError(`Prism language ${lang} is not loaded (loaded: ${languages}).`); | ||
} | ||
this.docinfoProcessor(function(){ | ||
this.process((doc) => { | ||
if (doc.backend !== 'html5') { | ||
return ''; | ||
} | ||
return `<pre class="highlight highlight-prismjs prismjs"><code class="language-${lang}" data-lang="${lang}">${node.getContent()}</code></pre>`; | ||
}, | ||
const theme = doc.getAttribute('prism-theme') || DEFAULT_THEME; | ||
highlight (node, content, lang) { | ||
return Prism.highlight(content, Prism.languages[lang]) | ||
.replace(/____(\d+)____/gi, '<b class="conum">($1)</b>'); | ||
}, | ||
if (!doc.hasAttribute('prism-theme') || !theme) { | ||
return ''; | ||
} | ||
handlesHighlighting () { | ||
return true; | ||
}, | ||
const prism_folder = dirname(require.resolve('prismjs')); | ||
const theme_location = join(prism_folder, 'themes', theme); | ||
const output = readFileSync(theme_location); | ||
hasDocinfo() { | ||
return true; | ||
}, | ||
return `<style type="text/css" class="prism-theme">${output}</style>`; | ||
}); | ||
}); | ||
}; | ||
docinfo () { | ||
if (this.backend !== 'html5') { | ||
return ''; | ||
} | ||
const theme = this.theme || DEFAULT_THEME; | ||
if (!this.theme || !theme) { | ||
return ''; | ||
} | ||
const prism_folder = dirname(require.resolve('prismjs')); | ||
const theme_location = join(prism_folder, 'themes', theme); | ||
const output = readFileSync(theme_location); | ||
return `<style type="text/css" class="prism-theme">${output}</style>`; | ||
} | ||
} |
{ | ||
"name": "asciidoctor-prism-extension", | ||
"version": "1.0.1", | ||
"version": "2.0.1", | ||
"description": "Highlight Asciidoc code listings with Prism.js, server side, with no front-end dependencies.", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"engines": { | ||
"node": ">=10.x.x" | ||
"node": ">=10" | ||
}, | ||
@@ -38,4 +38,7 @@ "keywords": [ | ||
"devDependencies": { | ||
"asciidoctor.js": "^1.5.9" | ||
"@asciidoctor/core": "^2.0.0-rc.3" | ||
}, | ||
"peerDependencies": { | ||
"@asciidoctor/core": "^2.0.0-rc.3" | ||
} | ||
} |
@@ -12,3 +12,3 @@ # asciidoctor-prism-extension [![Build Status](https://travis-ci.org/oncletom/asciidoctor-prism-extension.svg?branch=master)](https://travis-ci.org/oncletom/asciidoctor-prism-extension) | ||
```bash | ||
$ npm i asciidoctor.js asciidoctor-prism-extension | ||
$ npm install @asciidoctor/core asciidoctor-prism-extension | ||
``` | ||
@@ -21,6 +21,6 @@ | ||
```js | ||
const asciidoctor = require('asciidoctor.js')(); | ||
const asciidoctor = require('@asciidoctor/core')(); | ||
const prismExtension = require('asciidoctor-prism-extension'); | ||
asciidoctor.Extensions.register(prismExtension); | ||
asciidoctor.SyntaxHighlighter.register('prism', prismExtension); | ||
``` | ||
@@ -27,0 +27,0 @@ |
12
test.js
'use strict'; | ||
const asciidoctor = require('asciidoctor.js')(); | ||
const asciidoctor = require('@asciidoctor/core')(); | ||
const prismExtension = require('./index.js'); | ||
@@ -8,3 +8,3 @@ const assert = require('assert').strict; | ||
asciidoctor.Extensions.register(prismExtension); | ||
asciidoctor.SyntaxHighlighter.register('prism', prismExtension); | ||
@@ -29,7 +29,7 @@ const doc = `= Document | ||
'prism-theme=prism.css', | ||
'source-highlighter=prism', | ||
]; | ||
// Throw a TypeError if a source is converted without the backend being loaded | ||
assert.throws(() => asciidoctor.convert(doc, {backend}), TypeError); | ||
assert.throws(() => asciidoctor.convert(doc, {backend}), /(loaded: bash)/); | ||
assert.throws(() => asciidoctor.convert(doc, {backend, attributes: ['source-highlighter=prism']}), /(loaded: bash)/); | ||
@@ -41,3 +41,5 @@ // Loaded language makes the conversion | ||
assert.ok(output.match('<div class="listingblock prismjs highlight-prismjs">')); | ||
assert.ok(output.match('<div class="listingblock">')); | ||
assert.ok(output.match('<pre class="highlight highlight-prismjs prismjs">')); | ||
assert.ok(output.match('<code class="language-yaml" data-lang="yaml">')); | ||
assert.ok(output.match('<span class="token key atrule">')); | ||
@@ -44,0 +46,0 @@ assert.ok(!output.match('<style type="text/css" class="prism-theme">')); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
43659
119
2