├── .travis.yml ├── .gitignore ├── example.js ├── package.json ├── license ├── index.js ├── test.js └── readme.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 'stable' 4 | - '5' 5 | - '4' 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | coverage/ 3 | npm-debug.log* 4 | .nyc_output 5 | bundle.js 6 | .vscode -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var Markdown = require('.') 2 | var Highlight = require('highlight-syntax') 3 | var highlight = Highlight([ require('highlight-syntax/js') ]) 4 | var css = require('sheetify') 5 | css('highlight-syntax/light.css') 6 | 7 | var md = Markdown({ 8 | highlight: function (str, lang) { 9 | if (lang) { 10 | try { 11 | return highlight(str, { lang: lang }) 12 | } catch (__) {} 13 | } 14 | 15 | return '' 16 | } 17 | }) 18 | 19 | var dom = md` 20 | **Hello** _world_ 21 | 22 | \`\`\`js 23 | var hello = 'world' 24 | function print (str) { 25 | console.log(str) 26 | } 27 | \`\`\` 28 | ` 29 | document.body.appendChild(dom) 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "keywords": [ 3 | "tagged", 4 | "template", 5 | "literal", 6 | "parser", 7 | "markdown" 8 | ], 9 | "license": "MIT", 10 | "name": "marli", 11 | "description": "markdown tagged template literal renderer", 12 | "author": "YerkoPalma", 13 | "version": "2.0.0", 14 | "main": "index.js", 15 | "files": [ 16 | "index.js" 17 | ], 18 | "scripts": { 19 | "start": "budo -p 8080 -H 0.0.0.0 example.js -- -t sheetify", 20 | "test": "standard --verbose | snazzy && tape test.js | tap-summary" 21 | }, 22 | "repository": "YerkoPalma/marli", 23 | "devDependencies": { 24 | "assert-html": "^1.1.5", 25 | "bel": "^5.1.0", 26 | "budo": "^10.0.4", 27 | "highlight-syntax": "^3.0.2", 28 | "markdown-it-meta": "0.0.1", 29 | "sheetify": "^6.1.1", 30 | "snazzy": "^7.0.0", 31 | "standard": "^10.0.3", 32 | "tap-summary": "^4.0.0", 33 | "tape": "^4.8.0" 34 | }, 35 | "dependencies": { 36 | "markdown-it": "^8.4.0" 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 YerkoPalma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Markdownit = require('markdown-it') 2 | 3 | function Marli (presetName, opts) { 4 | if (typeof presetName === 'object') { 5 | opts = presetName 6 | presetName = 'default' 7 | } 8 | var rules 9 | if (opts && opts.rules) { 10 | rules = opts.rules 11 | delete opts.rules 12 | } 13 | var md = Markdownit(presetName, opts) 14 | if (opts && opts.plugins && Array.isArray(opts.plugins)) { 15 | opts.plugins.forEach(plugin => { 16 | md.use(plugin) 17 | }) 18 | } 19 | if (typeof rules === 'object') { 20 | for (var rule in rules) { 21 | md.renderer.rules[rule] = rules[rule] 22 | } 23 | } 24 | function _md (strings) { 25 | var arglen = arguments.length 26 | var result = '' 27 | for (var i = 0; i < arglen; i++) { 28 | var arg = arguments[ i + 1 ] || '' 29 | result += strings[i] + arg 30 | } 31 | if (typeof document !== 'undefined') { 32 | var div = document.createElement('div') 33 | div.innerHTML = md.render(result) 34 | return div 35 | } else { 36 | return md.render(result) 37 | } 38 | } 39 | _md._md = md 40 | return _md 41 | } 42 | module.exports = Marli 43 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var assertHtml = require('assert-html') 3 | var html = require('bel') 4 | 5 | test('render html', function (t) { 6 | var Markdown = require('./') 7 | var md = Markdown() 8 | var dom = html`
Hello world
` 9 | var mdDom = md`**Hello** _world_` 10 | assertHtml(t, dom.toString(), mdDom) 11 | t.end() 12 | }) 13 | 14 | test('render html with argumetns', function (t) { 15 | var Markdown = require('./') 16 | var md = Markdown() 17 | var friend = 'Peter' 18 | var friendLastName = 'Pan' 19 | var dom = html`Hello ${friend} ${friendLastName}
` 20 | var mdDom = md`**Hello** _${friend} ${friendLastName}_` 21 | assertHtml(t, dom.toString(), mdDom) 22 | t.end() 23 | }) 24 | 25 | test('allow access to override rules', function (t) { 26 | var defaultRender = function (tokens, idx, options, env, self) { 27 | return self.renderToken(tokens, idx, options) 28 | } 29 | 30 | function link_open (tokens, idx, options, env, self) { // eslint-disable-line camelcase 31 | var aIndex = tokens[idx].attrIndex('target') 32 | 33 | if (aIndex < 0) { 34 | tokens[idx].attrPush(['target', '_blank']) 35 | } else { 36 | tokens[idx].attrs[aIndex][1] = '_blank' 37 | } 38 | return defaultRender(tokens, idx, options, env, self) 39 | } 40 | var marli = require('./')({ rules: { link_open } }) 41 | var dom = html`` 42 | var mdDom = marli`[google](www.google.com)` 43 | assertHtml(t, dom.toString(), mdDom) 44 | t.end() 45 | }) 46 | 47 | test('allow passing plugins to Markdown-it', function (t) { 48 | var Markdown = require('./') 49 | var md = Markdown({plugins: [require('markdown-it-meta')]}) 50 | 51 | md`--- 52 | title: Welcome to Markdown-it-meta 53 | keywords: markdown-it-meta 54 | --- 55 | ## Hello World` 56 | t.deepEqual(md._md.meta, { 57 | title: 'Welcome to Markdown-it-meta', 58 | keywords: 'markdown-it-meta' 59 | }) 60 | t.end() 61 | }) 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # marli 2 | [](https://travis-ci.org/YerkoPalma/marli) [](https://github.com/feross/standard) 3 | 4 | > **mar**kdown tagged template **li**teral render 5 | 6 | ## Install 7 | 8 | ```bash 9 | $ npm install --save marli 10 | ``` 11 | 12 | ## Usage 13 | 14 | ```js 15 | var Markdown = require('marli') 16 | var md = Markdown() 17 | 18 | var dom = md` 19 | **Hello** _world_ 20 | --- 21 | ` 22 | document.body.appendChild(dom) 23 | ``` 24 | 25 | ## API 26 | 27 | ### md = Markdown([presetName][, options]) 28 | 29 | Marli uses [markdown-it][markdown-it] under the hood, so it accepts the same 30 | arguments for it's constructor. Check [markdown-it constructor][constructor] 31 | docs for more info. You can also pass a rules object as an option property, 32 | that will be passed to the [renderer][renderer], so you can overrider rules 33 | like this 34 | 35 | ```js 36 | function link_open (tokens, idx, options, env, self) { // eslint-disable-line camelcase 37 | var aIndex = tokens[idx].attrIndex('target') 38 | 39 | if (aIndex < 0) { 40 | tokens[idx].attrPush(['target', '_blank']) 41 | } else { 42 | tokens[idx].attrs[aIndex][1] = '_blank' 43 | } 44 | return defaultRender(tokens, idx, options, env, self) 45 | } 46 | var md = require('marli')({ rules: { link_open } }) 47 | var mdDom = marli`[google](www.google.com)` 48 | // outputs 49 | // 50 | ``` 51 | 52 | In the same way you can pass plugins to Markdown-it constructor, like this 53 | 54 | ```js 55 | var Markdown = require('./') 56 | var md = Markdown({plugins: [require('markdown-it-meta')]}) 57 | ``` 58 | ## License 59 | [MIT](/license) 60 | 61 | [markdown-it]: https://github.com/markdown-it/markdown-it 62 | [constructor]: https://markdown-it.github.io/markdown-it/#MarkdownIt.new 63 | [renderer]: https://github.com/markdown-it/markdown-it/blob/3353462142d519dfe5b613e4d9e79fa29601ff98/lib/renderer.js --------------------------------------------------------------------------------