Skip to content

Commit

Permalink
autodetect highlight language
Browse files Browse the repository at this point in the history
  • Loading branch information
jhchen committed Jul 12, 2016
1 parent 5ac2696 commit 9ea959d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
9 changes: 5 additions & 4 deletions assets/base.styl
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,8 @@ colorItemsPerRow = 7
margin-bottom: 5px
margin-top: 5px
padding-left: 16px
pre.hljs
overflow: visible
code, pre:not(.hljs)
background-color: #f0f0f0
code, pre
background-color: #f0f0f0
border-radius: 3px
pre
white-space: pre-wrap
Expand All @@ -138,6 +135,10 @@ colorItemsPerRow = 7
&:before, &:after
content: "\00a0"
letter-spacing: -2px
pre.ql-syntax
background-color: #23241f
color: #f8f8f2;
overflow: visible
img
max-width: 100%

Expand Down
30 changes: 10 additions & 20 deletions modules/syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@ import CodeBlock from '../formats/code';


class SyntaxCodeBlock extends CodeBlock {
static create(value) {
let domNode = super.create(value);
if (typeof value === 'string') {
domNode.dataset.language = value;
} else {
domNode.dataset.language = SyntaxCodeBlock.DEFAULT_LANGUAGE;
}
domNode.classList.add(domNode.dataset.language);
return domNode;
}

static formats(domNode) {
return domNode.dataset.language || SyntaxCodeBlock.DEFAULT_LANGUAGE;
}

replaceWith(block) {
this.domNode.textContent = this.domNode.textContent;
this.attach();
Expand All @@ -30,15 +15,14 @@ class SyntaxCodeBlock extends CodeBlock {
if (this.cachedHTML !== this.domNode.innerHTML) {
let text = this.domNode.textContent;
if (text.trim().length > 0 || this.cachedHTML == null) {
this.domNode.textContent = text;
highlight(this.domNode);
this.domNode.innerHTML = highlight(text);
this.attach();
}
this.cachedHTML = this.domNode.innerHTML;
}
}
}
SyntaxCodeBlock.DEFAULT_LANGUAGE = 'javascript';
SyntaxCodeBlock.className = 'ql-syntax';


let CodeToken = new Parchment.Attributor.Class('token', 'hljs', {
Expand All @@ -50,7 +34,7 @@ class Syntax extends Module {
constructor(quill, options) {
super(quill, options);
if (typeof this.options.highlight !== 'function') {
throw new Error('Syntax module requires highlight.js. Please include the library on the page.');
throw new Error('Syntax module requires highlight.js. Please include the library on the page before Quill.');
}
Quill.register(CodeToken, true);
Quill.register(SyntaxCodeBlock, true);
Expand All @@ -77,7 +61,13 @@ class Syntax extends Module {
}
}
Syntax.DEFAULTS = {
highlight: (window.hljs != null ? window.hljs.highlightBlock: null)
highlight: (function() {
if (window.hljs == null) return null;
return function(text) {
let result = window.hljs.highlightAuto(text);
return result.value;
}
})()
};


Expand Down

0 comments on commit 9ea959d

Please sign in to comment.