Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added wrap line function

  • Loading branch information...
commit c1b83b86a85020cbab73a46d1af6253fdcfdc3f3 1 parent b4378b3
James Morrin authored

Showing 1 changed file with 30 additions and 10 deletions. Show diff stats Hide diff stats

  1. +30 10 tasks/markdown.js
40 tasks/markdown.js
@@ -8,8 +8,8 @@
8 8
9 9 module.exports = function(grunt) {
10 10 'use strict';
11   -
12   - var path = require('path');
  11 +
  12 + var path = require('path');
13 13 var markdown = require('marked');
14 14 var hljs = require('highlight.js');
15 15 var _ = require('lodash');
@@ -17,11 +17,28 @@ module.exports = function(grunt) {
17 17 grunt.registerHelper('markdown', function(src, options, template) {
18 18
19 19 var html = null;
  20 + var codeLines = options.codeLines;
  21 + var shouldWrap = codeLines && codeLines.before && codeLines.after;
  22 +
  23 + function wrapLines(code) {
  24 + var out = [];
  25 + var before = codeLines.before;
  26 + var after = codeLines.after;
  27 + code = code.split('\n');
  28 + code.forEach(function(line) {
  29 + out.push(before+line+after);
  30 + });
  31 + return out.join('\n');
  32 + }
20 33
21 34 if(typeof options.highlight === 'string') {
22 35 if(options.highlight === 'auto') {
23 36 options.highlight = function(code) {
24   - return hljs.highlightAuto(code).value;
  37 + var out = hljs.highlightAuto(code).value;
  38 + if(shouldWrap) {
  39 + out = wrapLines(out);
  40 + }
  41 + return out;
25 42 };
26 43 } else if (options.highlight === 'manual') {
27 44 options.highlight = function(code, lang) {
@@ -31,15 +48,18 @@ module.exports = function(grunt) {
31 48 } catch(e) {
32 49 out = hljs.highlightAuto(code).value;
33 50 }
  51 + if(shouldWrap) {
  52 + out = wrapLines(out);
  53 + }
34 54 return out;
35 55 };
36 56 }
37   -
  57 +
38 58 }
39 59
40 60 markdown.setOptions(options);
41 61
42   - grunt.verbose.write('Marking down...');
  62 + grunt.verbose.write('Marking down...');
43 63
44 64 html = markdown(src);
45 65
@@ -51,21 +71,21 @@ module.exports = function(grunt) {
51 71 var destPath = this.data.dest;
52 72 var options = this.data.options || {};
53 73 var extension = this.data.extenstion || 'html';
54   - var templateFn = this.data.template || path.join(__dirname, 'template.html');
  74 + var templateFn = this.data.template || path.join(__dirname, 'template.html');
55 75 var template = grunt.file.read(templateFn);
56 76
57 77 grunt.file.expandFiles(this.data.files).forEach(function(filepath) {
58   -
  78 +
59 79 var file = grunt.file.read(filepath);
60 80
61 81 var html = grunt.helper('markdown', file, options, template);
62 82 var ext = path.extname(filepath);
63 83 var dest = path.join(destPath, path.basename(filepath, ext) +'.'+ extension);
64 84 grunt.file.write(dest, html);
65   -
  85 +
66 86 });
67   -
  87 +
68 88 });
69   -
  89 +
70 90 };
71 91

0 comments on commit c1b83b8

Please sign in to comment.
Something went wrong with that request. Please try again.