├── test ├── text-pre-headings.md ├── text.md ├── text-multi.md └── index.js ├── .travis.yml ├── .gitignore ├── package.json ├── LICENSE ├── index.js └── README.md /test/text-pre-headings.md: -------------------------------------------------------------------------------- 1 | Hello World 2 | 3 | # Main Content 4 | 5 | Goes here 6 | -------------------------------------------------------------------------------- /test/text.md: -------------------------------------------------------------------------------- 1 | ## my heading 2 | oh wow, amazing 3 | 4 | ## another heading 5 | gorgeous copy, stunning 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "0.10" 3 | - "1" 4 | sudo: false 5 | language: node_js 6 | script: "npm run test-cov" 7 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 8 | -------------------------------------------------------------------------------- /test/text-multi.md: -------------------------------------------------------------------------------- 1 | # first heading 2 | 3 | Hi there! 4 | 5 | This content runs over multiple lines... 6 | 7 | ## second heading 8 | 9 | As does this one. 10 | Yup. 11 | 12 | 13 | 14 | With even more whitespace :O 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # tmp files 2 | lib-cov 3 | *.seed 4 | *.log 5 | *.csv 6 | *.dat 7 | *.out 8 | *.pid 9 | *.gz 10 | 11 | # tmp folders 12 | pids/ 13 | logs/ 14 | results/ 15 | coverage/ 16 | 17 | # node.js 18 | node_modules/ 19 | npm-debug.log 20 | 21 | # osx 22 | .DS_Store 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mdjson", 3 | "version": "2.0.1", 4 | "description": "Transform markdown to an object where headings are keys", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "standard && NODE_ENV=test node test/index.js", 8 | "test-cov": "standard && NODE_ENV=test istanbul cover test/index.js" 9 | }, 10 | "repository": "yoshuawuyts/mdjson", 11 | "keywords": [ 12 | "markdown", 13 | "parse", 14 | "json" 15 | ], 16 | "license": "MIT", 17 | "dependencies": { 18 | "mdast": "^0.26.0", 19 | "mdast-html": "^0.1.0" 20 | }, 21 | "devDependencies": { 22 | "istanbul": "^0.3.13", 23 | "standard": "^3.6.1", 24 | "tape": "^4.0.0" 25 | }, 26 | "files": [ 27 | "LICENSE", 28 | "index.js", 29 | "README.md" 30 | ] 31 | } 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 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 all 13 | 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 THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const html = require('mdast-html') 2 | const assert = require('assert') 3 | const mdast = require('mdast') 4 | 5 | module.exports = mdjson 6 | 7 | // map a markdown string to an object 8 | // with `html` and `raw` fields 9 | // str -> obj 10 | function mdjson (txt) { 11 | assert.equal(typeof txt, 'string', 'input should be a markdown string') 12 | 13 | const toHtml = mdast().use(html) 14 | const lexer = mdast() 15 | const tokens = lexer.parse(txt).children 16 | const res = {} 17 | var key = '' 18 | 19 | tokens.forEach(function (token, i) { 20 | if (token.type === 'heading') { 21 | key = token.children[0].value 22 | res[key] = [] 23 | return 24 | } 25 | 26 | if (!key) return 27 | 28 | res[key].push(token) 29 | }) 30 | 31 | Object.keys(res).forEach(function (key) { 32 | const tree = { 33 | type: 'root', 34 | children: res[key] 35 | } 36 | 37 | res[key] = { 38 | raw: trimRight(lexer.stringify(tree)), 39 | html: trimRight(toHtml.stringify(tree)) 40 | } 41 | }) 42 | 43 | return res 44 | } 45 | 46 | // trim whitespace at the 47 | // end of a string 48 | // str -> str 49 | function trimRight (value) { 50 | return value.replace(/\n+$/, '') 51 | } 52 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const path = require('path') 2 | const test = require('tape') 3 | const fs = require('fs') 4 | 5 | const mdjson = require('../') 6 | 7 | test('should assert input types', function (t) { 8 | t.plan(1) 9 | t.throws(mdjson, /markdown string/) 10 | }) 11 | 12 | test('should return a parsed obj', function (t) { 13 | t.plan(2) 14 | fs.readFile(path.resolve(__dirname, 'text.md'), 'utf8', function (err, res) { 15 | t.notOk(err) 16 | const obj = mdjson(res) 17 | t.deepLooseEqual(obj, { 18 | 'my heading': { 19 | raw: 'oh wow, amazing', 20 | html: '
oh wow, amazing
' 21 | }, 22 | 'another heading': { 23 | raw: 'gorgeous copy, stunning', 24 | html: 'gorgeous copy, stunning
' 25 | } 26 | }) 27 | }) 28 | }) 29 | 30 | test('should handle multiple paragraphs', function (t) { 31 | const fixture = path.resolve(__dirname, 'text-multi.md') 32 | const markdown = fs.readFileSync(fixture, 'utf8') 33 | const tree = mdjson(markdown) 34 | 35 | t.deepEqual(tree, { 36 | 'first heading': { 37 | html: 'Hi there!
\nThis content runs over multiple lines...
', 38 | raw: ' Hi there!\n\nThis content runs over multiple lines...' 39 | }, 40 | 'second heading': { 41 | html: 'As does this one.\nYup.
\nWith even more whitespace :O
', 42 | raw: 'As does this one.\nYup.\n\nWith even more whitespace :O' 43 | } 44 | }) 45 | 46 | t.end() 47 | }) 48 | 49 | test('should ignore text before headings', function (t) { 50 | const fixture = path.resolve(__dirname, 'text-pre-headings.md') 51 | const markdown = fs.readFileSync(fixture, 'utf8') 52 | const tree = mdjson(markdown) 53 | 54 | t.deepEqual(tree, { 55 | 'Main Content': { 56 | html: 'Goes here
', 57 | raw: 'Goes here' 58 | } 59 | }) 60 | 61 | t.end() 62 | }) 63 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mdjson 2 | [![NPM version][npm-image]][npm-url] 3 | [![build status][travis-image]][travis-url] 4 | [![Test coverage][coveralls-image]][coveralls-url] 5 | [![Downloads][downloads-image]][downloads-url] 6 | [![js-standard-style][standard-image]][standard-url] 7 | 8 | Transform markdown to an object where headings are keys. 9 | 10 | ## Installation 11 | ```bash 12 | $ npm install mdjson 13 | ``` 14 | 15 | ## Usage 16 | ```js 17 | const mdjson = require('mdjson') 18 | 19 | mdjson(` 20 | This part (before any headers) is ignored. Feel free 21 | to use this section for commentary on the file's purpose, 22 | if you wish. 23 | 24 | ## my heading 25 | oh wow, amazing 26 | 27 | ## another heading 28 | gorgeous copy, stunning 29 | `) 30 | // => { 31 | // 'my heading': { 32 | // raw: 'oh wow, amazing', 33 | // html: 'oh wow, amazing
' 34 | // }, 35 | // 'another heading': { 36 | // raw: 'gorgeous copy, stunning', 37 | // html: 'gorgeous copy, stunning
' 38 | // } 39 | //} 40 | ``` 41 | 42 | ## Why? 43 | Writing copy in markdown is more pleasant than writing it inline in html or JS. 44 | This module allows you to separate copy from markup on a page per page basis. 45 | 46 | ## See Also 47 | - [newspeak](https://github.com/yoshuawuyts/newspeak) - Natural language localization 48 | - [ndjson](https://github.com/maxogden/ndjson) - newline delimited json parser, not to be confused with this markdown module 49 | 50 | ## License 51 | [MIT](https://tldrlegal.com/license/mit-license) 52 | 53 | [npm-image]: https://img.shields.io/npm/v/mdjson.svg?style=flat-square 54 | [npm-url]: https://npmjs.org/package/mdjson 55 | [travis-image]: https://img.shields.io/travis/yoshuawuyts/mdjson.svg?style=flat-square 56 | [travis-url]: https://travis-ci.org/yoshuawuyts/mdjson 57 | [coveralls-image]: https://img.shields.io/coveralls/yoshuawuyts/mdjson.svg?style=flat-square 58 | [coveralls-url]: https://coveralls.io/r/yoshuawuyts/mdjson?branch=master 59 | [downloads-image]: http://img.shields.io/npm/dm/mdjson.svg?style=flat-square 60 | [downloads-url]: https://npmjs.org/package/mdjson 61 | [standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square 62 | [standard-url]: https://github.com/feross/standard 63 | --------------------------------------------------------------------------------