├── .gitignore ├── .npmignore ├── History.md ├── Readme.md ├── cli.js ├── example.js ├── index.js ├── package.json └── test.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | support 2 | test 3 | examples 4 | *.sock 5 | .travis.yml 6 | Makefile 7 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 0.1.0 / 2014-10-13 3 | ================== 4 | 5 | * index: Add blockquote, hr and codespan 6 | 7 | 0.0.1 / 2014-03-06 8 | ================== 9 | 10 | * Initial commit 11 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # ansimd 2 | 3 | Markdown to ANSI for your terminal 4 | 5 | ## JS Usage 6 | 7 | ```js 8 | var fs = require('fs'); 9 | var read = fs.readFileSync; 10 | var render = require('ansimd'); 11 | 12 | var str = read('./Readme.md'); 13 | console.log(render(str)); 14 | ``` 15 | 16 | ## CLI usage 17 | 18 | You can also install `ansimd` using the `-g` flag and use it from the CLI directly: 19 | 20 | ``` 21 | npm install ansimd -g 22 | ansimd foo.md 23 | ``` 24 | 25 | ## License 26 | 27 | (The MIT License) 28 | 29 | Copyright (c) 2014 Stephen Mathieson <me@stephenmathieson.com> 30 | 31 | Permission is hereby granted, free of charge, to any person obtaining 32 | a copy of this software and associated documentation files (the 33 | 'Software'), to deal in the Software without restriction, including 34 | without limitation the rights to use, copy, modify, merge, publish, 35 | distribute, sublicense, and/or sell copies of the Software, and to 36 | permit persons to whom the Software is furnished to do so, subject to 37 | the following conditions: 38 | 39 | The above copyright notice and this permission notice shall be 40 | included in all copies or substantial portions of the Software. 41 | 42 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 43 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 44 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 45 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 46 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 47 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 48 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var ansimd = require('./') 4 | var fs = require('fs') 5 | 6 | var args = process.argv.slice(2) 7 | if (!args[0]) { 8 | console.error('Usage: ansimd ') 9 | process.exit(1) 10 | } 11 | 12 | var file = fs.readFileSync(args[0]) 13 | 14 | console.log(ansimd(file.toString())) 15 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 2 | var fs = require('fs'); 3 | var read = fs.readFileSync; 4 | var render = require('./'); 5 | 6 | var str = read('./Readme.md'); 7 | console.log(render(str)); 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | /** 3 | * Module dependencies. 4 | */ 5 | 6 | var marked = require('marked'); 7 | var unescape = require('unescape-html'); 8 | 9 | /** 10 | * Expose `render`. 11 | */ 12 | 13 | exports = module.exports = render; 14 | 15 | /** 16 | * Render the given markdown `text`. 17 | */ 18 | 19 | function render(text) { 20 | text = String(text); 21 | var str = marked(text, { renderer: renderer }); 22 | // remove escaped things (<, >, etc.) 23 | return unescape(str); 24 | } 25 | 26 | /** 27 | * Expose our `renderer`. 28 | */ 29 | 30 | var renderer = exports.renderer = {}; 31 | 32 | /** 33 | * Render text. 34 | */ 35 | 36 | renderer.text = function (text) { 37 | return text; 38 | }; 39 | 40 | /** 41 | * Render tablecell. 42 | */ 43 | 44 | renderer.tablecell = function (text) { 45 | return text; 46 | }; 47 | 48 | /** 49 | * Render tablerow. 50 | */ 51 | 52 | renderer.tablerow = function (text) { 53 | return text; 54 | }; 55 | 56 | /** 57 | * Render table. 58 | */ 59 | 60 | renderer.table = function (text) { 61 | return text; 62 | }; 63 | 64 | /** 65 | * Render a heading. 66 | */ 67 | 68 | renderer.heading = function (text) { 69 | return newlines(bold(underline(text))); 70 | }; 71 | 72 | /** 73 | * Render a code block. 74 | */ 75 | 76 | renderer.code = function (text) { 77 | return newlines(text.replace(/^/gm, ' ')); 78 | }; 79 | 80 | /** 81 | * Render italic text. 82 | */ 83 | 84 | renderer.em = function (text) { 85 | return italic(text); 86 | }; 87 | 88 | /** 89 | * Render strong text. 90 | */ 91 | 92 | renderer.strong = 93 | renderer.codespan = function (text) { 94 | return bold(text); 95 | }; 96 | 97 | /** 98 | * Render a list. 99 | */ 100 | 101 | renderer.list = function (text, ordered) { 102 | var i = 0; 103 | return newlines(text 104 | .split('\0') 105 | .filter(Boolean) 106 | .map(function (item) { 107 | var prefix = ordered ? ' ' + ++i + '. ' : ' - '; 108 | return prefix + item; 109 | }) 110 | .join('\n')); 111 | }; 112 | 113 | /** 114 | * Render a list item. 115 | */ 116 | 117 | renderer.listitem = function (text) { 118 | return text + '\0'; 119 | }; 120 | 121 | /** 122 | * Render a paragraph. 123 | */ 124 | 125 | renderer.paragraph = newlines; 126 | 127 | /** 128 | * Render (noop) images and linebreaks. 129 | */ 130 | 131 | renderer.image = 132 | renderer.hr = 133 | renderer.blockquote = 134 | renderer.br = newline; 135 | 136 | /** 137 | * Render a link. 138 | */ 139 | 140 | renderer.link = function (href, title, text) { 141 | return underline(text) + ' (' + href + ')'; 142 | }; 143 | 144 | /** 145 | * Wrap the given `str` in newlines. 146 | */ 147 | 148 | function newlines(str) { 149 | return '\n' + str + '\n'; 150 | } 151 | 152 | /** 153 | * Newline. 154 | */ 155 | 156 | function newline() { 157 | return '\n'; 158 | } 159 | 160 | /** 161 | * Make `str` bold. 162 | */ 163 | 164 | function bold(str) { 165 | return '\033[1m' + str + '\033[0m'; 166 | } 167 | 168 | /** 169 | * Underline `str`. 170 | */ 171 | 172 | function underline(str) { 173 | return '\033[4m' + str + '\033[0m'; 174 | } 175 | 176 | /** 177 | * Make `str` italic. 178 | */ 179 | 180 | function italic(str) { 181 | return '\033[3m' + str + '\033[0m'; 182 | } 183 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ansimd", 3 | "version": "0.2.1", 4 | "description": "Markdown to ANSI for your terminal", 5 | "keywords": [], 6 | "bin": { 7 | "ansimd": "cli.js" 8 | }, 9 | "author": "Stephen Mathieson ", 10 | "repository": { 11 | "type": "git", 12 | "url": "git://github.com/stephenmathieson/node-ansimd.git" 13 | }, 14 | "dependencies": { 15 | "marked": "0.3.4", 16 | "unescape-html": "~1.0.0" 17 | }, 18 | "devDependencies": {}, 19 | "main": "index", 20 | "bugs": { 21 | "url": "https://github.com/stephenmathieson/node-ansimd/issues" 22 | }, 23 | "homepage": "https://github.com/stephenmathieson/node-ansimd", 24 | "scripts": { 25 | "test": "node cli.js test.md" 26 | }, 27 | "license": "MIT" 28 | } 29 | -------------------------------------------------------------------------------- /test.md: -------------------------------------------------------------------------------- 1 | # An exhibit of Markdown 2 | 3 | This note demonstrates some of what [Markdown][1] is capable of doing. 4 | 5 | *Note: Feel free to play with this page. Unlike regular notes, this doesn't automatically save itself.* 6 | 7 | ## Basic formatting 8 | 9 | Paragraphs can be written like so. A paragraph is the basic block of Markdown. A paragraph is what text will turn into when there is no reason it should become anything else. 10 | 11 | Paragraphs must be separated by a blank line. Basic formatting of *italics* and **bold** is supported. This *can be **nested** like* so. 12 | 13 | ## Lists 14 | 15 | ### Ordered list 16 | 17 | 1. Item 1 18 | 2. A second item 19 | 3. Number 3 20 | 4. Ⅳ 21 | 22 | *Note: the fourth item uses the Unicode character for [Roman numeral four][2].* 23 | 24 | ### Unordered list 25 | 26 | * An item 27 | * Another item 28 | * Yet another item 29 | * And there's more... 30 | 31 | ## Paragraph modifiers 32 | 33 | ### Code block 34 | 35 | Code blocks are very useful for developers and other people who look at code or other things that are written in plain text. As you can see, it uses a fixed-width font. 36 | 37 | You can also make `inline code` to add code into other things. 38 | 39 | ### Quote 40 | 41 | > Here is a quote. What this is should be self explanatory. Quotes are automatically indented when they are used. 42 | 43 | ## Headings 44 | 45 | There are six levels of headings. They correspond with the six levels of HTML headings. You've probably noticed them already in the page. Each level down uses one more hash character. 46 | 47 | ### Headings *can* also contain **formatting** 48 | 49 | ### They can even contain `inline code` 50 | 51 | Of course, demonstrating what headings look like messes up the structure of the page. 52 | 53 | I don't recommend using more than three or four levels of headings here, because, when you're smallest heading isn't too small, and you're largest heading isn't too big, and you want each size up to look noticeably larger and more important, there there are only so many sizes that you can use. 54 | 55 | ## URLs 56 | 57 | URLs can be made in a handful of ways: 58 | 59 | * A named link to [MarkItDown][3]. The easiest way to do these is to select what you want to make a link and hit `Ctrl+L`. 60 | * Another named link to [MarkItDown](http://www.markitdown.net/) 61 | * Sometimes you just want a URL like . 62 | 63 | ## Horizontal rule 64 | 65 | A horizontal rule is a line that goes across the middle of the page. 66 | 67 | --- 68 | 69 | It's sometimes handy for breaking things up. 70 | 71 | ## Images 72 | 73 | Markdown can also contain images. I'll need to add something here sometime. 74 | 75 | ## Finally 76 | 77 | There's actually a lot more to Markdown than this. See the official [introduction][4] and [syntax][5] for more information. However, be aware that this is not using the official implementation, and this might work subtly differently in some of the little things. 78 | 79 | 80 | [1]: http://daringfireball.net/projects/markdown/ 81 | [2]: http://www.fileformat.info/info/unicode/char/2163/index.htm 82 | [3]: http://www.markitdown.net/ 83 | [4]: http://daringfireball.net/projects/markdown/basics 84 | [5]: http://daringfireball.net/projects/markdown/syntax 85 | 86 | Here is a cool list 87 | 88 | - http://cats.com 89 | - http://dogs.com 90 | 91 | --------------------------------------------------------------------------------