javascript - convert markdown to json object -


i have markdown file imported in node module through webpack loader

import mardownfile './markdownfile.md'

this file text book chapters separated ## / h2 tag

now, i'm looking way convert json object each h2 tag (or other possible wrapper) in separate chapter chunks use react page component page content props.children. more details on i'm trying solve

i have in markdown.md file

#title ##chapter 1 text text text ##chapter 2 text etc ##chapter 3 more text image 

i read markdown , convert object, this...

var atext = { pages: [ {  "title": "chapter 1.",  "text": "text", }, { "title": "chapter 2.", "text": "text", }, { "title": "chapter 3.", "text": "text", "img": "cat-stevens.png", } ]} 

then in javascript react component render page component this

<page page={atext.pages[0]} />

i'm on mac osx computer personal web client project, trying parse in standard browser, i'm using chrome, what's best approach accomplish this, suggestions?

you can not import md file, because import on javascript files. need use markdown parser or write own. example markdown-it:

var markdownit = require('markdown-it'); var md = new markdownit(); var result = md.parse('# markdown-it rulezz!'); console.log(result); 

you get:

[token {   type: 'heading_open',   tag: 'h1',   attrs: null,   map: [0, 1],   nesting: 1,   level: 0,   children: null,   content: '',   markup: '#',   info: '',   meta: null,   block: true,   hidden: false },   token {   type: 'inline',   tag: '',   attrs: null,   map: [0, 1],   nesting: 0,   level: 1,   children: [[object]],   content: 'markdown-it rulezz!',   markup: '',   info: '',   meta: null,   block: true,   hidden: false },   token {   type: 'heading_close',   tag: 'h1',   attrs: null,   map: null,   nesting: -1,   level: 0,   children: null,   content: '',   markup: '#',   info: '',   meta: null,   block: true,   hidden: false }] 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -