使用NodeJ将Json文件转换为Markdown

时间:2018-08-30 10:32:16

标签: json node.js markdown

我有一个问题。我有一个JSON文件,我需要使用NodeJs将该文件转换为Markdown格式,以及如何实现?

第一个块标题第二个块代码。

我只需要code个区块:

{
    "code": "## Chapter 21 - Dichotomous Predicted Variable",
    "language": "Markdown",
    "index": 0,
    "output": {
        "image/png": "aaaaaaa",
        "text/plain": [
            "<IPython.core.display.Image object>"
        ]
    },
    "created": "2018-8-28 12:03:33",
    "evaluated": true,
    "hiddenCode": false
},
{
    "code": "- [21.1 - Multiple Metric Predictors](#21.1---Multiple-Metric-Predictors)\n- [21.3 - Robust Logistic Regression](#21.3---Robust-Logistic-Regression)\n- [21.4 - Nominal Predictors](#21.4---Nominal-Predictors)",
    "language": "Python",
    "index": 0,
    "output": {
        "image/png": "bbbbb",
        "text/plain": [
            "<IPython.core.display.Image object>"
        ]
    },
    "created": "2018-8-28 12:03:33",
    "evaluated": true,
    "hiddenCode": false
},

我想说的是,我将全部打印成纯文本格式,最后得到.md输出。我会把它弄清楚;

if language == Markdown

[21.1 - Multiple Metric Predictors](#21.1---Multiple-Metric-Predictors)\n- [21.3 - Robust Logistic Regression](#21.3---Robust-Logistic-Regression)\n- [21.4 - Nominal Predictors](#21.4---Nominal-Predictors)",


if language === Python

%load std_ipython_import.txt
\nimport pandas as pd\n
import numpy as np\n
import matplotlib.pyplot as plt\n
import seaborn as sns\n
import pymc3 as pm\n
import theano.tensor as tt\n
\nfrom scipy.stats import beta\n
from scipy.special import expit\n
from matplotlib import gridspec\n
from IPython.display import Image\n\n
%matplotlib inline\nplt.style.use('seaborn-white')\n\ncolor = '#87ceeb'\n\nf_dict = {'size':16}",

1 个答案:

答案 0 :(得分:0)

您将需要一些库来进行转换。例如showdownhttp://showdownjs.com)。然后很简单,只需map遍历数组并转换所需的内容即可。

// index.js
const showdown = require('showdown')

const json = [
  {
    "code": "## Chapter 21 - Dichotomous Predicted Variable",
    ...
  },
  {
    "code": "- [21.1 - Multiple Metric Predictors](#21.1---Multiple-Metric-Predictors)\n- [21.3 - Robust Logistic Regression](#21.3---Robust-Logistic-Regression)\n- [21.4 - Nominal Predictors](#21.4---Nominal-Predictors)",
    ...
  },
]

const converter = new showdown.Converter()
const result = json.map(item => converter.makeHtml(item.code))

console.log(result)

输出:

[ 
  '<h2 id="chapter21dichotomouspredictedvariable">Chapter 21 - Dichotomous Predicted Variable</h2>',
  '<ul>\n<li><a href="#21.1---Multiple-Metric-Predictors">21.1 - Multiple Metric Predictors</a></li>\n<li><a href="#21.3---Robust-Logistic-Regression">21.3 - Robust Logistic Regression</a></li>\n<li><a href="#21.4---Nominal-Predictors">21.4 - Nominal Predictors</a></li>\n</ul>' 
]