metalsmith-prism 4.2.2 → 4.2.4
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 +59 -55
- package/package.json +11 -11
package/lib/index.js
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
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
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
+
|
|
9
|
+
const loadLanguages = require( 'prismjs/components/' );
|
|
10
|
+
loadLanguages( [ 'php' ] );
|
|
11
|
+
|
|
12
|
+
const he = require( 'he' );
|
|
13
|
+
|
|
14
|
+
const isHTMLFile = ( filePath ) => {
|
|
15
|
+
return /\.html|\.htm/.test( extname( filePath ) );
|
|
12
16
|
};
|
|
13
17
|
|
|
14
18
|
|
|
@@ -19,20 +23,20 @@ const isHTMLFile = (filePath) => {
|
|
|
19
23
|
* @returns
|
|
20
24
|
*/
|
|
21
25
|
|
|
22
|
-
module.exports = (options) => {
|
|
26
|
+
module.exports = ( options ) => {
|
|
23
27
|
|
|
24
28
|
options = options || {};
|
|
25
29
|
|
|
26
|
-
if (options.preLoad) {
|
|
30
|
+
if ( options.preLoad ) {
|
|
27
31
|
//list of available languages: https://github.com/PrismJS/prism/tree/master/components
|
|
28
|
-
options.preLoad.forEach((language) => {
|
|
32
|
+
options.preLoad.forEach( ( language ) => {
|
|
29
33
|
try {
|
|
30
|
-
require(`prismjs/components/prism-${language}.js`);
|
|
31
|
-
} catch (e) {
|
|
34
|
+
require( `prismjs/components/prism-${ language }.js` );
|
|
35
|
+
} catch ( e ) {
|
|
32
36
|
/* eslint no-console: 0 */
|
|
33
|
-
console.warn(`Failed to preload prism syntax: ${language} !`);
|
|
37
|
+
console.warn( `Failed to preload prism syntax: ${ language } !` );
|
|
34
38
|
}
|
|
35
|
-
});
|
|
39
|
+
} );
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
/**
|
|
@@ -41,13 +45,13 @@ module.exports = (options) => {
|
|
|
41
45
|
*
|
|
42
46
|
* @param {*} language
|
|
43
47
|
*/
|
|
44
|
-
function requireLanguage(language) {
|
|
45
|
-
if (!languages[language]) {
|
|
48
|
+
function requireLanguage( language ) {
|
|
49
|
+
if ( !languages[ language ] ) {
|
|
46
50
|
try {
|
|
47
|
-
require(`prismjs/components/prism-${language}.js`);
|
|
48
|
-
} catch (e) {
|
|
51
|
+
require( `prismjs/components/prism-${ language }.js` );
|
|
52
|
+
} catch ( e ) {
|
|
49
53
|
/* eslint no-console: 0 */
|
|
50
|
-
console.warn(`Failed to load prism syntax: ${language} !`);
|
|
54
|
+
console.warn( `Failed to load prism syntax: ${ language } !` );
|
|
51
55
|
}
|
|
52
56
|
}
|
|
53
57
|
}
|
|
@@ -65,69 +69,69 @@ module.exports = (options) => {
|
|
|
65
69
|
const NEW_LINE_EXP = /\n(?!$)/g;
|
|
66
70
|
let lineNumbersWrapper;
|
|
67
71
|
|
|
68
|
-
Prism.hooks.add('after-tokenize', function
|
|
69
|
-
const match = env.code.match(NEW_LINE_EXP);
|
|
72
|
+
Prism.hooks.add( 'after-tokenize', function( env ) {
|
|
73
|
+
const match = env.code.match( NEW_LINE_EXP );
|
|
70
74
|
const linesNum = match ? match.length + 1 : 1;
|
|
71
|
-
const lines = new Array(linesNum + 1).join('<span></span>');
|
|
75
|
+
const lines = new Array( linesNum + 1 ).join( '<span></span>' );
|
|
72
76
|
|
|
73
|
-
lineNumbersWrapper = `<span aria-hidden="true" class="line-numbers-rows">${lines}</span>`;
|
|
74
|
-
});
|
|
77
|
+
lineNumbersWrapper = `<span aria-hidden="true" class="line-numbers-rows">${ lines }</span>`;
|
|
78
|
+
} );
|
|
75
79
|
|
|
76
|
-
return function(files, metalsmith, done) {
|
|
80
|
+
return function( files, metalsmith, done ) {
|
|
77
81
|
|
|
78
|
-
setImmediate(done);
|
|
82
|
+
setImmediate( done );
|
|
79
83
|
|
|
80
|
-
Object.keys(files).forEach(file => {
|
|
84
|
+
Object.keys( files ).forEach( file => {
|
|
81
85
|
|
|
82
|
-
if (!isHTMLFile(file)) {
|
|
86
|
+
if ( !isHTMLFile( file ) ) {
|
|
83
87
|
return;
|
|
84
88
|
}
|
|
85
89
|
|
|
86
|
-
const contents = files[file].contents.toString();
|
|
87
|
-
const $ = cheerio.load(contents, { decodeEntities: false }, true);
|
|
90
|
+
const contents = files[ file ].contents.toString();
|
|
91
|
+
const $ = cheerio.load( contents, { decodeEntities: false }, true );
|
|
88
92
|
let highlighted = false;
|
|
89
|
-
const code = $('code');
|
|
93
|
+
const code = $( 'code' );
|
|
90
94
|
|
|
91
|
-
if (!code.length) return;
|
|
95
|
+
if ( !code.length ) return;
|
|
92
96
|
|
|
93
|
-
code.each(function() {
|
|
94
|
-
const $this = $(this);
|
|
95
|
-
const className = $this.attr('class') || '';
|
|
96
|
-
const targets = className.split('language-');
|
|
97
|
+
code.each( function() {
|
|
98
|
+
const $this = $( this );
|
|
99
|
+
const className = $this.attr( 'class' ) || '';
|
|
100
|
+
const targets = className.split( 'language-' );
|
|
97
101
|
let addLineNmbers = false;
|
|
98
102
|
|
|
99
|
-
if (targets.length > 1) {
|
|
103
|
+
if ( targets.length > 1 ) {
|
|
100
104
|
|
|
101
|
-
const $pre = $this.parent('pre');
|
|
105
|
+
const $pre = $this.parent( 'pre' );
|
|
102
106
|
|
|
103
|
-
if ($pre) {
|
|
107
|
+
if ( $pre ) {
|
|
104
108
|
// Copy className to <pre> container
|
|
105
|
-
$pre.addClass(className);
|
|
109
|
+
$pre.addClass( className );
|
|
106
110
|
|
|
107
|
-
if (options.lineNumbers) {
|
|
108
|
-
debug('adding line numbers');
|
|
109
|
-
$pre.addClass('line-numbers');
|
|
111
|
+
if ( options.lineNumbers ) {
|
|
112
|
+
debug( 'adding line numbers' );
|
|
113
|
+
$pre.addClass( 'line-numbers' );
|
|
110
114
|
addLineNmbers = true;
|
|
111
115
|
}
|
|
112
116
|
}
|
|
113
117
|
|
|
114
118
|
highlighted = true;
|
|
115
|
-
let language = targets[1];
|
|
116
|
-
requireLanguage(language);
|
|
119
|
+
let language = targets[ 1 ];
|
|
120
|
+
requireLanguage( language );
|
|
117
121
|
|
|
118
|
-
if (!languages[language]) {
|
|
122
|
+
if ( !languages[ language ] ) {
|
|
119
123
|
language = 'markup';
|
|
120
124
|
}
|
|
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);
|
|
125
|
+
const html = ( language === 'markup' && !options.decode ) ? $this.html() : he.decode( $this.html() );
|
|
126
|
+
const highlightedCode = Prism.highlight( html, Prism.languages[ language ] );
|
|
127
|
+
$this.html( addLineNmbers ? highlightedCode + lineNumbersWrapper : highlightedCode );
|
|
124
128
|
|
|
125
129
|
}
|
|
126
|
-
});
|
|
130
|
+
} );
|
|
127
131
|
|
|
128
|
-
if (highlighted) {
|
|
129
|
-
files[file].contents = Buffer.from($.html());
|
|
132
|
+
if ( highlighted ) {
|
|
133
|
+
files[ file ].contents = Buffer.from( $.html() );
|
|
130
134
|
}
|
|
131
|
-
});
|
|
135
|
+
} );
|
|
132
136
|
};
|
|
133
137
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "metalsmith-prism",
|
|
3
|
-
"version": "4.2.
|
|
3
|
+
"version": "4.2.4",
|
|
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
|
}
|