Home > Software engineering >  Reading a markdown file as a variable and converting the variable to json
Reading a markdown file as a variable and converting the variable to json

Time:11-15

I´m trying to convert a local md file to json in my API, but I get syntax errors.

The problem seems to be that when I read the file and store it as a string variable, it gets stored without the new rows that are required by md2json to work. When I console log the variable it gets printed line by line looking just like the md file, but I cannot convert it. When I send it as a respone and print it on my page it prints:

# Air ## The second largest heading ###### The smallest heading

But the original md file looks like this:

# Air

## The second largest heading

###### The smallest heading

This is what I´ve tried:

const md2json = require('md-2-json');
const fs = require('fs');

router.get('/page', (req, res) => {
    let content = fs.readFileSync('directory/file.md','utf8')
    console.log(content)
    res.send(content)
})

Is there some way to make this work, or is there some other better way to do it? I saw some posts about XMLHttpRequest but didn´t really get it to work.

//* Edit: I now saw that I forgot to include the converting; this is what it´s supposed to look like when using md2json:

router.get('/page', (req, res) => {
    let content = fs.readFileSync('directory/file.md','utf8')
    console.log(content)
    let newContent = md2json.parse(content)
    console.log(newContent)
    res.send(newContent)
})

//* Edit 2: The lack of \n doesn´t seem to be the problem, since this works:

let mdInput = `# Air ## The second largest heading ###### The smallest heading`

router.get('/page', (req, res) => {
    let newContent = md2json.parse(mdInput)
    console.log(newContent)
    res.send(newContent)

But this doesn´t:

let mdInput = `
# Air

## The second largest heading
 
##### The smallest heading
`

router.get('/page', (req, res) => {
    let newContent = md2json.parse(mdInput)
    console.log(newContent)
    res.send(newContent)

CodePudding user response:

I got it : it is because this lib seems to allow h(n) to live only in h(n-1) so a h6 can't be directly in a h2, so this should solve your problem :

# Air

## The second largest heading

### The third largest heading
#### The fourth largest heading
##### The second smallest heading

###### The smallest heading
const fs = require('fs');

router.get('/page', (req, res) => {
    const content = fs.readFileSync('directory/file.md','utf8').toString()
    let newContent = md2json.parse(content)
    res.end(`<!DOCTYPE html><html><head></head><body>${ newContent }</body></html>`)
})

CodePudding user response:

Your page prints everything in one line because html ignores \n as a new line character by default

# Air

## The second largest heading

###### The smallest heading

You have to wrap your text with pre tag

<pre>
# Air

## The second largest heading

###### The smallest heading
</pre>

You can also use white-space: pre css property or replace \n with <br>

CodePudding user response:

Maybe you need to send it with a little bit of html :

const md2json = require('md-2-json');
const fs = require('fs');

router.get('/page', (req, res) => {
    const content = fs.readFileSync('directory/file.md','utf8').toString()
    let newContent = md2json.parse(content)
    res.end(`<!DOCTYPE html><html><head></head><body>${ newContent }</body></html>`)
})
  • Related