Permalink
Browse files

change highlight option. add code class prefix.

  • Loading branch information...
1 parent 0e2ce6b commit 041dd98ed46c7609ac39d32e6452571cd247b716 @chjj committed Apr 6, 2012
Showing with 19 additions and 44 deletions.
  1. +9 −34 README.md
  2. +8 −8 lib/marked.js
  3. +1 −1 test/new/gfm_code.html
  4. +1 −1 test/tests/gfm_code.html
View
@@ -68,6 +68,7 @@ marked has 3 different switches which change behavior.
Don't fix any of the original markdown bugs or poor behavior.
- __gfm__: Enable github flavored markdown (enabled by default).
- __sanitize__: Sanitize the output. Ignore any HTML that has been input.
+- __highlight__: A callback to highlight code blocks.
None of the above are mutually exclusive/inclusive.
@@ -78,7 +79,14 @@ None of the above are mutually exclusive/inclusive.
marked.setOptions({
gfm: true,
pedantic: false,
- sanitize: true
+ sanitize: true,
+ // callback for code highlighter
+ highlight: function(code, language) {
+ if (language === 'js') {
+ code = highlight(code);
+ }
+ return code;
+ }
});
console.log(marked('i am using __markdown__.'));
```
@@ -110,39 +118,6 @@ $ cat hello.html
<p>hello world</p>
```
-## Syntax Highlighting
-
-Marked has an interface that allows for a syntax highlighter to highlight code
-blocks before they're output.
-
-Example implementation:
-
-``` js
-var highlight = require('my-syntax-highlighter')
- , marked = require('marked');
-
-marked.highlight = function(text) {
- var tokens = marked.lexer(text)
- , l = tokens.length
- , i = 0
- , token;
-
- for (; i < l; i++) {
- token = tokens[i];
- if (token.type === 'code') {
- token.text = highlight(token.text, token.lang);
- // Tell marked that this
- // token is already escaped.
- token.escaped = true;
- }
- }
-
- return marked.parser(tokens);
-};
-
-module.exports = marked;
-```
-
## License
Copyright (c) 2011-2012, Christopher Jeffrey. (MIT License)
View
@@ -531,19 +531,19 @@ var tok = function() {
+ '>\n';
}
case 'code': {
+ if (!token.escaped) {
+ token.text = options.highlight
+ ? options.highlight(token.text, token.lang)
+ : escape(token.text, true);
+ }
return '<pre><code'
+ (token.lang
- ? ' class="'
+ ? ' class="lang-'
+ token.lang
+ '"'
: '')
+ '>'
- + (options.highlighter
- ? options.highlighter(token.text)
- : (token.escaped
- ? token.text
- : escape(token.text, true))
- )
+ + token.text
+ '</code></pre>\n';
}
case 'blockquote_start': {
@@ -748,7 +748,7 @@ marked.options({
gfm: true,
pedantic: false,
sanitize: false,
- highlighter: null
+ highlight: null
});
/**
View
@@ -1,3 +1,3 @@
-<pre><code class="js">var a = &#39;hello&#39;;
+<pre><code class="lang-js">var a = &#39;hello&#39;;
console.log(a + &#39; world&#39;);
</code></pre>
View
@@ -1,3 +1,3 @@
-<pre><code class="js">var a = &#39;hello&#39;;
+<pre><code class="lang-js">var a = &#39;hello&#39;;
console.log(a + &#39; world&#39;);
</code></pre>

0 comments on commit 041dd98

Please sign in to comment.