Skip to content

Commit 734bac4

Browse files
author
Christopher Baker
authored
Prism fixes (#17)
* create prism-collapse plugin * fix prism styles, add line-highlight, trim trailing newlines, highlight html template strings * refactor collapse and collapse line numbers * collapse handles highlights * disable prism auto-run and fix highlight color * do not upgrade naked code * remove highlight and collapse code (moved to bit-docs-html-highlight-line)
1 parent a147cd6 commit 734bac4

File tree

5 files changed

+94
-39
lines changed

5 files changed

+94
-39
lines changed

package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"test": "mocha test.js --reporter spec",
88
"postversion": "git push --tags && git push",
99
"preversion": "npm test",
10-
"release:pre": "npm version prerelease && npm publish",
10+
"release:pre": "npm version prerelease && npm publish --tag=pre",
1111
"release:patch": "npm version patch && npm publish",
1212
"release:minor": "npm version minor && npm publish",
1313
"release:major": "npm version major && npm publish"
@@ -29,8 +29,7 @@
2929
"bit-docs-generate-html": "^0.1.0",
3030
"connect": "^2.14.4",
3131
"mocha": "^3.1.2",
32-
"q": "^1.4.1",
33-
"zombie": "^4.2.1"
32+
"zombie": "^4.3.0"
3433
},
3534
"dependencies": {
3635
"prismjs": "^1.11.0"

prettify.js

+40-27
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,62 @@
11
require("./prettify.less");
22

3+
require("./prism-config");
34
var Prism = require("prismjs");
4-
require("prismjs/plugins/line-numbers/prism-line-numbers.js");
5-
require("prismjs/plugins/previewers/prism-previewers.js");
6-
require("prismjs/plugins/command-line/prism-command-line.js");
7-
85
require("prismjs/themes/prism-coy.css");
6+
7+
require("prismjs/plugins/line-numbers/prism-line-numbers");
98
require("prismjs/plugins/line-numbers/prism-line-numbers.css");
9+
10+
require("prismjs/plugins/previewers/prism-previewers");
1011
require("prismjs/plugins/previewers/prism-previewers.css");
12+
13+
require("prismjs/plugins/command-line/prism-command-line");
1114
require("prismjs/plugins/command-line/prism-command-line.css");
1215

13-
/**
14-
* @parent bit-docs-prettify/static
15-
* @module {function} bit-docs-prettify/prettify.js
16-
*
17-
* Main front end JavaScript file for static portion of this plugin.
18-
*
19-
* @signature `prettyPrint()`
20-
*
21-
* Finds all `<code>` elements on the page and adds the `prettyprint` class
22-
* before executing the required pretty print engine.
23-
*
24-
* Also requires [bit-docs-prettify/prettify.less].
25-
*/
26-
module.exports = function(){
16+
require("prismjs/plugins/toolbar/prism-toolbar");
17+
require("prismjs/plugins/toolbar/prism-toolbar.css");
18+
require("prismjs/plugins/copy-to-clipboard/prism-copy-to-clipboard");
19+
20+
Prism.languages.insertBefore('javascript', 'template-string', {
21+
'html-template-string': {
22+
pattern: /`(?:[\s\S])*<[a-z-]+(?:\s+[^<>]*)?>(?:[\s\S])*`/,
23+
inside: {
24+
'interpolation': {
25+
pattern: /\$\{[^}]+\}/,
26+
inside: {
27+
'interpolation-punctuation': {
28+
pattern: /^\$\{|\}$/,
29+
alias: 'punctuation'
30+
},
31+
rest: Prism.languages.javascript
32+
}
33+
},
34+
rest: Prism.languages.html
35+
}
36+
}
37+
});
38+
39+
module.exports = function() {
2740
var codes = document.getElementsByTagName("code");
2841
for (var i = 0; i < codes.length; i++) {
2942
var code = codes[i];
3043

44+
if (code.textContent.slice(-1) === "\n") {
45+
code.textContent = code.textContent.slice(0, -1);
46+
}
47+
3148
if (code.parentNode.nodeName.toUpperCase() === "PRE") {
3249
code.parentNode.className += " line-numbers";
3350

34-
if (!code.className.includes("language-")) {
35-
code.className += " language-unknown";
36-
}
37-
3851
if (code.className.includes("language-shell")) {
3952
code.parentNode.className += " command-line";
53+
}
4054

41-
if (code.textContent.slice(-1) === "\n") {
42-
code.textContent = code.textContent.slice(0, -1);
43-
}
55+
if (!code.className.includes("language-")) {
56+
code.className += " language-unknown";
4457
}
4558
}
4659
}
4760

48-
Prism.highlightAll();
49-
}
61+
window.requestAnimationFrame(Prism.highlightAll);
62+
};

prettify.less

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
pre[class*="language-"] > code {
2-
border-left: none !important;
3-
box-shadow: 0px 0px 0px 1px #dfdfdf !important;
1+
article {
2+
pre[class*="language-"] > code {
3+
font-size: 16px;
4+
margin: 0;
5+
border-left: none;
6+
box-shadow: 0px 0px 0px 1px #dfdfdf;
7+
background-image: none;
8+
}
9+
10+
pre[class*=language-]:after, pre[class*=language-]:before {
11+
display: none;
12+
content: none;
13+
}
14+
15+
.line-highlight {
16+
background: rgba(0, 180, 0, .1);
17+
background: linear-gradient(to right, rgba(0, 180, 0, .1) 70%, rgba(0, 180, 0, 0));
18+
}
419
}

prism-config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
window.Prism = {
2+
manual: true
3+
};

test.js

+31-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,38 @@
11
var generate = require("bit-docs-generate-html/generate");
2-
var Q = require("q");
32
var path = require("path");
3+
var fs = require("fs");
44
var assert = require("assert");
55

66
var Browser = require("zombie");
77
var connect = require("connect");
88

9+
function zombieFixes() {
10+
Object.defineProperty(HTMLElement.prototype, 'classList', {
11+
get: function() {
12+
var parent = this;
13+
14+
var classList = parent.className.split(' ');
15+
classList.contains = classList.includes;
16+
classList.add = function(token) {
17+
this.push(token);
18+
parent.className = this.join(' ');
19+
};
20+
21+
return classList;
22+
}
23+
});
24+
}
25+
zombieFixes = zombieFixes.toString() + "\nzombieFixes();\n\n"
26+
927
var open = function(url, callback, done) {
10-
var server = connect().use(connect.static(path.join(__dirname))).listen(8081);
28+
var stealPath = path.join(__dirname, "temp", "static", "node_modules", "steal", "steal.production.js");
29+
fs.writeFileSync(stealPath, zombieFixes + fs.readFileSync(stealPath, "utf8"));
30+
31+
var indexPath = path.join(__dirname, "temp", "index.html");
32+
fs.writeFileSync(indexPath, fs.readFileSync(indexPath, "utf8").replace("</head>", "<script src='https:\/\/cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script></head>"));
33+
34+
35+
var server = connect().use(connect.static(path.join(__dirname, 'temp'))).listen(8081);
1136
var browser = new Browser();
1237

1338
browser.visit("http://localhost:8081/" + url)
@@ -27,7 +52,7 @@ describe("bit-docs-prettify", function() {
2752
it("basics work", function(done) {
2853
this.timeout(30000);
2954

30-
var docMap = Q({
55+
var docMap = Promise.resolve({
3156
index: {
3257
name: "index",
3358
body: "```javascript\nvar str = 'hello world';\n```\n\n```css\nbody {\n margin: 0;\n background: purple;\n}\n```\n\n```shell\npwd\n```\n\n```\n// some misc code\n```"
@@ -45,13 +70,13 @@ describe("bit-docs-prettify", function() {
4570
forceBuild: true
4671
})
4772
.then(function() {
48-
open("temp/index.html", function(browser, close) {
73+
open("index.html", function(browser, close) {
4974
var codes = browser.window.document.getElementsByTagName("code");
5075

5176
for (var i = 0; i < codes.length; i++) {
5277
assert.ok(codes[i].className.includes("language-"), "has a language");
5378

54-
if (codes[i].parentNode.nodeName === "pre") {
79+
if (codes[i].parentNode.nodeName === "PRE") {
5580
assert.ok(codes[i].parentNode.className.includes("language-"), "parent has a language");
5681
}
5782
}
@@ -61,7 +86,7 @@ describe("bit-docs-prettify", function() {
6186
}, done);
6287
})
6388
.catch(function(err) {
64-
console.log("err", err.stack);
89+
console.log(err);
6590
done(err);
6691
})
6792
;

0 commit comments

Comments
 (0)