Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added doc & helpers

  • Loading branch information...
commit 06068dd2d0098af26b55893262526fe068bfc28f 1 parent 54432b6
@naholyr authored
Showing with 167 additions and 42 deletions.
  1. +81 −0 README.md
  2. +27 −0 helpers.js
  3. +20 −0 highlight.js
  4. +2 −42 index.js
  5. +37 −0 parser.js
View
81 README.md
@@ -0,0 +1,81 @@
+# Punch Markdown + Highlight
+
+This is a plugin for [Punch](https://github.com/laktek/punch), bringing syntax highlighting along Markdown parser.
+
+## Installation
+
+```
+npm install punch-markdown-highlight
+```
+
+## Usage
+
+### Parser
+
+In your `config.json`:
+
+```json
+{
+ "plugins": {
+ "parsers": {
+ ".md": "punch-markdown-highlight/parser",
+ ".markdown": "punch-markdown-highlight/parser"
+ }
+ }
+}
+```
+
+You can then use the three-backticks syntax (GitHub-like), and enjoy syntax highlighting, in all your Markdown contents.
+
+### Helper
+
+Whenever you would need to use the same feature in your templates, there is a bundler helper. Add this in your `config.json`:
+
+```json
+{
+ "plugins": {
+ "helpers": {
+ "markdown_highlight": "punch-markdown-highlight/helpers"
+ }
+ }
+}
+```
+
+You'll then be able to use the helper "`highlight`", this way:
+
+```mustache
+<pre>
+{{#highlight}}javascript
+alert('hello');
+{{/highlight}}
+</pre>
+```
+
+As you can't pass arguments to helpers, you'll have to provide the language as first part of the text, followed by a newline.
+
+## CSS
+
+Highlighter will only wrap tokens in specific classes, you have to add some CSS to your bundles.
+
+```less
+pre {
+ border: 1px solid #e0ded3;
+ border-radius: 4px;
+ margin: 10px 10px 40px 10px;
+ padding: 10px;
+ background-color: #f0efe8;
+ overflow: auto;
+ font-size: 14px;
+ font-family: Consolas, "Liberation Mono", Courier, monospace;
+}
+
+code {
+ white-space: pre;
+ color: rgba(0,0,0, 1);
+ .keyword { font-weight: bold; color: #6089d4; }
+ .string, .regexp { color: green }
+ .class, .special { color: #6089d4 }
+ .number { color: red }
+ .comment { color: grey }
+}
+```
View
27 helpers.js
@@ -0,0 +1,27 @@
+var highlight = require('./highlight');
+
+var tag_helpers = {};
+
+var block_helpers = {
+
+ highlight: function (text) {
+ var m = String(text).match(/^[a-z0-9_\-]+/);
+ if (m) {
+ var lang = m[0];
+ text = text.substring(lang.length).replace(/^[\r\n]*/g, '');
+ text = '<code class="lang-' + lang + '">' + highlight(text, lang) + '</code>';
+ }
+ return text;
+ }
+
+};
+
+module.exports = {
+ directAccess: function(){
+ return { "tag_helpers": tag_helpers, "block_helpers": block_helpers, "options": {} };
+ },
+ get: function(basepath, file_extension, options, callback){
+ var self = this;
+ return callback(null, { "tag": tag_helpers, "block": block_helpers }, {});
+ }
+};
View
20 highlight.js
@@ -0,0 +1,20 @@
+var highlighter = require('highlight.js');
+
+module.exports = function _highlight (code, lang) {
+ if (!lang) {
+ return code;
+ }
+
+ if (!highlighter.LANGUAGES[lang]) {
+ console.warn('Markdown Highlighter: unknown language "' + lang + '"');
+ return code;
+ }
+
+ try {
+ code = highlighter.highlight(lang, code).value;
+ } catch (e) {
+ console.error('Markdown Highlight Error', e);
+ }
+
+ return code;
+};
View
44 index.js
@@ -1,44 +1,4 @@
-var marked = require('marked');
-var highlight = require('highlight.js').highlight;
-var _ = require('lodash');
-
-var options;
-
module.exports = {
-
- supportedExtensions: [".markdown", ".md"],
-
- parse: function (input, cb){
- var output, err;
-
- try {
- marked.setOptions(options);
- output = marked.parse(String(input));
- } catch(error){
- err = error;
- }
-
- return cb(err, output);
- },
-
- setup: function (config) {
- options = {
- gfm: true,
- pedantic: false,
- sanitize: false,
- highlight: function _highlight (code, lang) {
- try {
- code = highlight(lang, code).value;
- } catch (e) {
- console.error('Markdown Highlight Error for language "' + lang + '"');
- }
- return code;
- }
- };
-
- if (config.parser) {
- options = _.extend(options, config.parser.markdown || {});
- }
- }
-
+ parser: require('./parser'),
+ helpers: require('./helpers')
};
View
37 parser.js
@@ -0,0 +1,37 @@
+var marked = require('marked');
+var highlight = require('./highlight');
+var _ = require('lodash');
+
+var options;
+
+module.exports = {
+
+ supportedExtensions: [".markdown", ".md"],
+
+ parse: function (input, cb){
+ var output, err;
+
+ try {
+ marked.setOptions(options);
+ output = marked.parse(String(input));
+ } catch(error){
+ err = error;
+ }
+
+ return cb(err, output);
+ },
+
+ setup: function (config) {
+ options = {
+ gfm: true,
+ pedantic: false,
+ sanitize: false,
+ highlight: highlight
+ };
+
+ if (config.parser) {
+ options = _.extend(options, config.parser.markdown || {});
+ }
+ }
+
+};
Please sign in to comment.
Something went wrong with that request. Please try again.