metalsmith-prism 4.2.1 → 4.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +55 -53
- package/package.json +11 -11
package/lib/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const cheerio = require('cheerio');
|
|
4
|
-
const debug = require('debug')('metalsmith-prism');
|
|
5
|
-
const extname = require('path').extname;
|
|
6
|
-
const languages = require('prismjs').languages;
|
|
7
|
-
const Prism = require('prismjs');
|
|
8
|
-
const he = require('he');
|
|
9
|
-
|
|
10
|
-
const isHTMLFile = (filePath) => {
|
|
11
|
-
return /\.html|\.htm/.test(extname(filePath));
|
|
3
|
+
const cheerio = require( 'cheerio' );
|
|
4
|
+
const debug = require( 'debug' )( 'metalsmith-prism' );
|
|
5
|
+
const extname = require( 'path' ).extname;
|
|
6
|
+
const languages = require( 'prismjs' ).languages;
|
|
7
|
+
const Prism = require( 'prismjs' );
|
|
8
|
+
const he = require( 'he' );
|
|
9
|
+
|
|
10
|
+
const isHTMLFile = ( filePath ) => {
|
|
11
|
+
return /\.html|\.htm/.test( extname( filePath ) );
|
|
12
12
|
};
|
|
13
13
|
|
|
14
14
|
|
|
@@ -19,20 +19,20 @@ const isHTMLFile = (filePath) => {
|
|
|
19
19
|
* @returns
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
module.exports = (options) => {
|
|
22
|
+
module.exports = ( options ) => {
|
|
23
23
|
|
|
24
24
|
options = options || {};
|
|
25
25
|
|
|
26
|
-
if (options.preLoad) {
|
|
26
|
+
if ( options.preLoad ) {
|
|
27
27
|
//list of available languages: https://github.com/PrismJS/prism/tree/master/components
|
|
28
|
-
options.preLoad.forEach((language) => {
|
|
28
|
+
options.preLoad.forEach( ( language ) => {
|
|
29
29
|
try {
|
|
30
|
-
require(`prismjs/components/prism-${language}.js`);
|
|
31
|
-
} catch (e) {
|
|
30
|
+
require( `prismjs/components/prism-${ language }.js` );
|
|
31
|
+
} catch ( e ) {
|
|
32
32
|
/* eslint no-console: 0 */
|
|
33
|
-
console.warn(`Failed to preload prism syntax: ${language} !`);
|
|
33
|
+
console.warn( `Failed to preload prism syntax: ${ language } !` );
|
|
34
34
|
}
|
|
35
|
-
});
|
|
35
|
+
} );
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
@@ -41,13 +41,13 @@ module.exports = (options) => {
|
|
|
41
41
|
*
|
|
42
42
|
* @param {*} language
|
|
43
43
|
*/
|
|
44
|
-
function requireLanguage(language) {
|
|
45
|
-
if (!languages[language]) {
|
|
44
|
+
function requireLanguage( language ) {
|
|
45
|
+
if ( !languages[ language ] ) {
|
|
46
46
|
try {
|
|
47
|
-
require(`prismjs/components/prism-${language}.js`);
|
|
48
|
-
} catch (e) {
|
|
47
|
+
require( `prismjs/components/prism-${ language }.js` );
|
|
48
|
+
} catch ( e ) {
|
|
49
49
|
/* eslint no-console: 0 */
|
|
50
|
-
console.warn(`Failed to load prism syntax: ${language} !`);
|
|
50
|
+
console.warn( `Failed to load prism syntax: ${ language } !` );
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
}
|
|
@@ -65,67 +65,69 @@ module.exports = (options) => {
|
|
|
65
65
|
const NEW_LINE_EXP = /\n(?!$)/g;
|
|
66
66
|
let lineNumbersWrapper;
|
|
67
67
|
|
|
68
|
-
Prism.hooks.add('after-tokenize', function
|
|
69
|
-
const match = env.code.match(NEW_LINE_EXP);
|
|
68
|
+
Prism.hooks.add( 'after-tokenize', function( env ) {
|
|
69
|
+
const match = env.code.match( NEW_LINE_EXP );
|
|
70
70
|
const linesNum = match ? match.length + 1 : 1;
|
|
71
|
-
const lines = new Array(linesNum + 1).join('<span></span>');
|
|
71
|
+
const lines = new Array( linesNum + 1 ).join( '<span></span>' );
|
|
72
72
|
|
|
73
|
-
lineNumbersWrapper = `<span aria-hidden="true" class="line-numbers-rows">${lines}</span>`;
|
|
74
|
-
});
|
|
73
|
+
lineNumbersWrapper = `<span aria-hidden="true" class="line-numbers-rows">${ lines }</span>`;
|
|
74
|
+
} );
|
|
75
75
|
|
|
76
|
-
return function(files, metalsmith, done) {
|
|
76
|
+
return function( files, metalsmith, done ) {
|
|
77
77
|
|
|
78
|
-
setImmediate(done);
|
|
78
|
+
setImmediate( done );
|
|
79
79
|
|
|
80
|
-
Object.keys(files).forEach(file => {
|
|
80
|
+
Object.keys( files ).forEach( file => {
|
|
81
81
|
|
|
82
|
-
if (!isHTMLFile(file)) {
|
|
82
|
+
if ( !isHTMLFile( file ) ) {
|
|
83
83
|
return;
|
|
84
84
|
}
|
|
85
85
|
|
|
86
|
-
const contents = files[file].contents.toString();
|
|
87
|
-
const $ = cheerio.load(contents, { decodeEntities: false }, true);
|
|
86
|
+
const contents = files[ file ].contents.toString();
|
|
87
|
+
const $ = cheerio.load( contents, { decodeEntities: false }, true );
|
|
88
88
|
let highlighted = false;
|
|
89
|
+
const code = $( 'code' );
|
|
89
90
|
|
|
90
|
-
|
|
91
|
+
if ( !code.length ) return;
|
|
91
92
|
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
const
|
|
93
|
+
code.each( function() {
|
|
94
|
+
const $this = $( this );
|
|
95
|
+
const className = $this.attr( 'class' ) || '';
|
|
96
|
+
const targets = className.split( 'language-' );
|
|
95
97
|
let addLineNmbers = false;
|
|
96
98
|
|
|
97
|
-
if (targets.length > 1) {
|
|
99
|
+
if ( targets.length > 1 ) {
|
|
98
100
|
|
|
99
|
-
const $pre = $this.parent('pre');
|
|
101
|
+
const $pre = $this.parent( 'pre' );
|
|
100
102
|
|
|
101
|
-
if ($pre) {
|
|
103
|
+
if ( $pre ) {
|
|
102
104
|
// Copy className to <pre> container
|
|
103
|
-
$pre.addClass(className);
|
|
105
|
+
$pre.addClass( className );
|
|
104
106
|
|
|
105
|
-
if (options.lineNumbers) {
|
|
106
|
-
debug('adding line numbers');
|
|
107
|
-
$pre.addClass('line-numbers');
|
|
107
|
+
if ( options.lineNumbers ) {
|
|
108
|
+
debug( 'adding line numbers' );
|
|
109
|
+
$pre.addClass( 'line-numbers' );
|
|
108
110
|
addLineNmbers = true;
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
113
|
|
|
112
114
|
highlighted = true;
|
|
113
|
-
let language = targets[1];
|
|
114
|
-
requireLanguage(language);
|
|
115
|
+
let language = targets[ 1 ];
|
|
116
|
+
requireLanguage( language );
|
|
115
117
|
|
|
116
|
-
if (!languages[language]) {
|
|
118
|
+
if ( !languages[ language ] ) {
|
|
117
119
|
language = 'markup';
|
|
118
120
|
}
|
|
119
|
-
const html = (language === 'markup' && !options.decode) ? $this.html() : he.decode($this.html());
|
|
120
|
-
const highlightedCode = Prism.highlight(html, Prism.languages[language]);
|
|
121
|
-
$this.html(addLineNmbers ? highlightedCode + lineNumbersWrapper : highlightedCode);
|
|
121
|
+
const html = ( language === 'markup' && !options.decode ) ? $this.html() : he.decode( $this.html() );
|
|
122
|
+
const highlightedCode = Prism.highlight( html, Prism.languages[ language ] );
|
|
123
|
+
$this.html( addLineNmbers ? highlightedCode + lineNumbersWrapper : highlightedCode );
|
|
122
124
|
|
|
123
125
|
}
|
|
124
|
-
});
|
|
126
|
+
} );
|
|
125
127
|
|
|
126
|
-
if (highlighted) {
|
|
127
|
-
files[file].contents = Buffer.from($.html());
|
|
128
|
+
if ( highlighted ) {
|
|
129
|
+
files[ file ].contents = Buffer.from( $.html() );
|
|
128
130
|
}
|
|
129
|
-
});
|
|
131
|
+
} );
|
|
130
132
|
};
|
|
131
133
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metalsmith-prism",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.3",
|
|
4
4
|
"description": "Syntax highlighting for Metalsmith HTML templates using Prism.js",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"engines": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"preversion": "npm run test",
|
|
11
|
-
"test": "mocha ./tests/index.
|
|
11
|
+
"test": "mocha ./tests/index.mjs",
|
|
12
12
|
"lint": "eslint ./lib ./tests"
|
|
13
13
|
},
|
|
14
14
|
"repository": {
|
|
@@ -32,19 +32,19 @@
|
|
|
32
32
|
},
|
|
33
33
|
"homepage": "https://github.com/wernerglinka/metalsmith-prism",
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"cheerio": "^1.0.0
|
|
36
|
-
"debug": "^4.3.
|
|
35
|
+
"cheerio": "^1.0.0",
|
|
36
|
+
"debug": "^4.3.7",
|
|
37
37
|
"he": "^1.2.0",
|
|
38
|
-
"metalsmith": "^2.6.
|
|
38
|
+
"metalsmith": "^2.6.3",
|
|
39
39
|
"prismjs": "^1.29.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"babel-eslint": "^7.2.3",
|
|
43
|
-
"chai": "^
|
|
44
|
-
"eslint": "^
|
|
45
|
-
"eslint-config-prettier": "^
|
|
46
|
-
"eslint-plugin-prettier": "^
|
|
47
|
-
"mocha": "^10.2
|
|
48
|
-
"prettier": "^
|
|
43
|
+
"chai": "^5.1.2",
|
|
44
|
+
"eslint": "^9.14.0",
|
|
45
|
+
"eslint-config-prettier": "^9.1.0",
|
|
46
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
47
|
+
"mocha": "^10.8.2",
|
|
48
|
+
"prettier": "^3.3.3"
|
|
49
49
|
}
|
|
50
50
|
}
|