Skip to content

Commit 793cd3b

Browse files
committed
Merge branch 'fix-11' of https://github.com/obedm503/showdown-highlight into new-version
2 parents 089c468 + d21f768 commit 793cd3b

File tree

6 files changed

+659
-10
lines changed

6 files changed

+659
-10
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ If you are using this library in one of your projects, add it in this list. :spa
8181

8282

8383
- [`bloggify-markdown-highlight`](https://github.com/Bloggify/bloggify-markdown-highlight#readme) (by Bloggify)—Highlight code blocks in the Markdown code.
84+
- [`ember-showdown-highlight`](https://npmjs.com/package/ember-showdown-highlight)—The default blueprint for ember-cli addons.
8485
- [`markdown2dash`](https://github.com/eyworldwide/markdown2dash#readme) (by Bob)—Convert markdown files to Dash docset in CLI
86+
- [`md-srv`](https://npmjs.com/package/md-srv) (by aikei)—a simple server, converting served .md files to html
8587
- [`swanky`](https://github.com/swanky-docs/swanky#readme) (by Rod Leviton)—A simple, flexible and powerful tool for creating beautiful documentation.
88+
- [`trumpdoc`](https://npmjs.com/package/trumpdoc)—# Features - 😄 Suuuupppper easy to build. Do not need any shiz to build.(such as webpack) - 👍 Simple and powerful, pure React application - 😈 Manage your documentation like a president - 📄 code-block styles
8689

8790
## :scroll: License
8891

lib/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const decodeHtml = require("html-encoder-decoder").decode
44
, showdown = require("showdown")
55
, hljs = require("highlight.js")
6+
, classAttr = 'class="'
67
;
78

89
module.exports = function showdownHighlight () {
@@ -15,8 +16,16 @@ module.exports = function showdownHighlight () {
1516
, flags = "g"
1617
, replacement = (wholeMatch, match, left, right) => {
1718
match = decodeHtml(match);
18-
let lang = (left.match(/class=\"([^ \"]+)/) || [])[1]
19-
if (lang) {
19+
let lang = (left.match(/class=\"([^ \"]+)/) || [])[1];
20+
21+
if (left.includes(classAttr)) {
22+
let attrIndex = left.indexOf(classAttr) + classAttr.length;
23+
left = left.slice(0, attrIndex) + 'hljs ' + left.slice(attrIndex);
24+
} else {
25+
left = left.slice(0, -1) + ' class="hljs">';
26+
}
27+
28+
if (lang && hljs.getLanguage(lang)) {
2029
return left + hljs.highlight(lang, match).value + right;
2130
} else {
2231
return left + hljs.highlightAuto(match).value + right;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"description": "This package uses [`highlight.js`](https://highlightjs.org) to highlight code blocks in [Showdown](https://github.com/showdownjs/showdown) output. :rocket:"
5151
},
5252
"contributors": [
53-
"Cristiano Ribeiro <expedit@gmail.com> (https://github.com/expedit85)"
53+
"Cristiano Ribeiro <expedit@gmail.com> (https://github.com/expedit85)",
54+
"obedm503 (https://obedm503.github.io)"
5455
]
55-
}
56+
}

test/index.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const tester = require("tester")
66
;
77

88
tester.describe("showdown-highlight", t => {
9-
t.should("A Showdown extension for highlight the code blocks.", () => {
10-
// After requiring the module, use it as extension
11-
let converter = new showdown.Converter({
12-
extensions: [showdownHighlight]
13-
});
9+
// After requiring the module, use it as extension
10+
let converter = new showdown.Converter({
11+
extensions: [showdownHighlight]
12+
});
1413

14+
t.should("A Showdown extension for highlight the code blocks.", () => {
1515
// Now you can Highlight code blocks
1616
let html = converter.makeHtml(`
1717
\`\`\`js
@@ -22,6 +22,21 @@ sayHello("Hello World", "Johnny");
2222
\`\`\`
2323
`);
2424

25+
t.expect(html.includes('class="hljs js language-js"')).toEqual(true);
2526
t.expect(html.includes("hljs-string")).toEqual(true);
2627
});
28+
29+
t.should("work without code block language", () => {
30+
// Now you can Highlight code blocks
31+
let html = converter.makeHtml(`
32+
\`\`\`
33+
function sayHello (msg, who) {
34+
return \`\${who} says: msg\`;
35+
}
36+
sayHello("Hello World", "Johnny");
37+
\`\`\`
38+
`);
39+
40+
t.expect(html.includes('class="hljs"')).toEqual(true);
41+
});
2742
});

0 commit comments

Comments
 (0)