├── .gitignore ├── .npmignore ├── History.md ├── Makefile ├── Readme.md ├── index.js ├── package.json └── test ├── index.js ├── test.js └── test.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 2.0.1 / 2016-09-23 3 | ================== 4 | 5 | * support class passthrough 6 | 7 | 2.0.0 / 2016-09-23 8 | ================== 9 | 10 | * better rendering defaults 11 | 12 | 1.0.2 / 2016-09-22 13 | ================== 14 | 15 | * fix for the cli 16 | * standard format 17 | 18 | 1.0.1 / 2016-09-22 19 | ================== 20 | 21 | * bump deps 22 | 23 | 0.0.1 / 2010-01-03 24 | ================== 25 | 26 | * Initial release 27 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | test: 3 | @./node_modules/.bin/mocha \ 4 | --require should \ 5 | --reporter spec 6 | 7 | .PHONY: test -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # browserify-markdown 3 | 4 | Browserify transform for markdown files 5 | 6 | ## Usage 7 | 8 | ### CLI 9 | 10 | ```sh 11 | browserify -t [ browserify-markdown ] test/test.js 12 | ``` 13 | 14 | ### Example 15 | 16 | ```js 17 | var str = require('./chapter-1.md'); 18 | document.body.appendChild(str); 19 | ``` 20 | 21 | ## Installation 22 | 23 | ```bash 24 | npm install browserify-markdown 25 | ``` 26 | 27 | ## License 28 | 29 | MIT 30 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module Dependencies 3 | */ 4 | 5 | var highlight = require('highlight.js') 6 | var Remarkable = require('remarkable') 7 | var extname = require('path').extname 8 | var s2j = require('string-to-js') 9 | var through = require('through') 10 | 11 | /** 12 | * Export `markdown` 13 | */ 14 | 15 | module.exports = markdown 16 | 17 | /** 18 | * Default regexp 19 | */ 20 | 21 | var rtype = /^(md|markdown)$/ 22 | 23 | /** 24 | * Mappings between remarkable and highlight.js 25 | */ 26 | 27 | var language = { 28 | 'js': 'javascript', 29 | 'html': 'xml', 30 | 'shell': 'bash' 31 | } 32 | 33 | /** 34 | * Highlight configuration 35 | */ 36 | 37 | highlight.configure({ 38 | tabReplace: ' ' 39 | }) 40 | 41 | /** 42 | * Default remarkable configuration 43 | */ 44 | 45 | var md = new Remarkable({ 46 | html: true, // Enable HTML tags in source 47 | xhtmlOut: true, // Use '/' to close single tags (
) 48 | breaks: true, // Convert '\n' in paragraphs into
49 | langPrefix: 'lang ', // CSS language prefix for fenced blocks 50 | highlight: function (code, lang) { 51 | // differences between remarkable and highlight.js 52 | lang = (language[lang]) ? language[lang] : lang 53 | 54 | // Let's not let syntax highlighting kill anything 55 | try { 56 | return lang 57 | ? highlight.highlight(lang, code).value 58 | : highlight.highlightAuto(code).value 59 | } catch(e) {} 60 | 61 | return '' 62 | } 63 | }) 64 | 65 | /** 66 | * Initialize `transform` 67 | * 68 | * @param {Object} opts 69 | */ 70 | 71 | function markdown (file, opts) { 72 | if (opts) md.set(opts) 73 | var type = extension(file) 74 | if (!rtype.test(type)) return through() 75 | 76 | var data = '' 77 | return through(write, end) 78 | 79 | // write 80 | function write (buf) { 81 | data += buf 82 | } 83 | 84 | // end 85 | function end () { 86 | try { 87 | var src = s2j(md.render(data)) 88 | } catch (e) { 89 | this.emit('error', e) 90 | return 91 | } 92 | 93 | this.queue(src) 94 | this.queue(null) 95 | } 96 | } 97 | 98 | /** 99 | * Get the file extension 100 | * 101 | * @param {String} file 102 | * @return {String} 103 | */ 104 | 105 | function extension (file) { 106 | return extname(file).slice(1) 107 | } 108 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "browserify-markdown", 3 | "version": "2.0.1", 4 | "description": "Browserify transform for markdown files", 5 | "keywords": [ 6 | "browserify", 7 | "transform", 8 | "markdown" 9 | ], 10 | "author": "Matthew Mueller ", 11 | "repository": { 12 | "type": "git", 13 | "url": "git://github.com/lapwinglabs/browserify-markdown.git" 14 | }, 15 | "dependencies": { 16 | "highlight.js": "^9.7.0", 17 | "remarkable": "github:matthewmueller/remarkable#3b896af", 18 | "string-to-js": "0.0.1", 19 | "through": "^2.3.7", 20 | "through2": "2.0.1" 21 | }, 22 | "main": "index", 23 | "devDependencies": { 24 | "browserify": "^13.1.0" 25 | } 26 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var Browserify = require('browserify'); 2 | var markdown = require('../'); 3 | 4 | Browserify(__dirname + '/test.md') 5 | .transform(markdown) 6 | .bundle() 7 | .pipe(process.stdout); 8 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | let md = require('./test.md') 2 | console.log(md) 3 | -------------------------------------------------------------------------------- /test/test.md: -------------------------------------------------------------------------------- 1 | # Browserify Markdown 2 | 3 | Browserify plugin for markdown files 4 | 5 | ```bash 6 | npm install browserify-markdown 7 | ``` 8 | 9 | ## Usage 10 | 11 | ```js example 12 | var Browserify = require('browserify'); 13 | var markdown = require('../'); 14 | 15 | Browserify(__dirname + '/test.md') 16 | .transform(markdown()) 17 | .bundle() 18 | .pipe(process.stdout); 19 | ``` 20 | 21 | --- 22 | --------------------------------------------------------------------------------