Implement cspreview renderer

Signed-off-by: default avatarYukai Huang <yukaihuangtw@gmail.com>
parent 4fd62939
This diff is collapsed.
......@@ -16,6 +16,7 @@ import { stripTags } from '../../utils/string'
import getUIElements from './lib/editor/ui-elements'
import { emojifyImageDir } from './lib/editor/constants'
import { parseFenceCodeParams, serializeParamToAttribute } from './lib/markdown/utils'
import { renderCSVPreview } from './lib/renderer/csvpreview'
import markdownit from 'markdown-it'
import markdownitContainer from 'markdown-it-container'
......@@ -1167,6 +1168,12 @@ md.renderer.rules.fence = (tokens, idx, options, env, self) => {
if (info) {
langName = info.split(/\s+/g)[0]
if (langName === 'csvpreview') {
const params = parseFenceCodeParams(info)
return renderCSVPreview(token.content, params)
}
if (/!$/.test(info)) token.attrJoin('class', 'wrap')
token.attrJoin('class', options.langPrefix + langName.replace(/=$|=\d+$|=\+$|!$|=!$/, ''))
token.attrJoin('class', 'hljs')
......
......@@ -2768,7 +2768,9 @@ function updateViewInner () {
var lastMeta = md.meta
md.meta = {}
delete md.metaError
var rendered = md.render(value)
const mdEnv = {}
window.mdTokens = md.parse(value, mdEnv)
let rendered = md.renderer.render(window.mdTokens, md.options, mdEnv)
if (md.meta.type && md.meta.type === 'slide') {
var slideOptions = {
separator: '^(\r\n?|\n)---(\r\n?|\n)$',
......
import Papa from 'papaparse'
const safeParse = d => {
try {
return JSON.parse(d)
} catch (err) {
return d
}
}
export function renderCSVPreview (csv, options = {}, attr = '') {
const opt = Object.keys(options).reduce((acc, key) => {
return Object.assign(acc, {
[key]: safeParse(options[key])
})
}, {})
const results = Papa.parse(csv.trim(), opt)
if (opt.header) {
const fields = results.meta.fields
return `<table ${attr}>
<thead>
<tr>
${fields.map(f => `<th>${f}</th>`).join('')}
</tr>
</thead>
<tbody>
${results.data.map(d => `<tr>
${fields.map(f => `<td>${d[f]}</td>`).join('')}
</tr>`).join('')}
</tbody>
</table>`
} else {
return `<table ${attr}>
<tbody>
${results.data.map(d => `<tr>
${d.map(f => `<td>${f}</td>`).join('')}
</tr>`).join('')}
</tbody>
</table>`
}
}
......@@ -7,6 +7,8 @@ import markdownitContainer from 'markdown-it-container'
import { md } from '../extra'
import modeType from './modeType'
import appState from './appState'
import { renderCSVPreview } from './renderer/csvpreview'
import { parseFenceCodeParams } from './markdown/utils'
function addPart (tokens, idx) {
if (tokens[idx].map && tokens[idx].level === 0) {
......@@ -66,6 +68,18 @@ md.renderer.rules.fence = (tokens, idx, options, env, self) => {
if (info) {
langName = info.split(/\s+/g)[0]
if (langName === 'csvpreview') {
const params = parseFenceCodeParams(info)
let attr = ''
if (tokens[idx].map && tokens[idx].level === 0) {
const startline = tokens[idx].map[0] + 1
const endline = tokens[idx].map[1]
attr = `class="part" data-startline="${startline}" data-endline="${endline}"`
}
return renderCSVPreview(token.content, params, attr)
}
if (/!$/.test(info)) token.attrJoin('class', 'wrap')
token.attrJoin('class', options.langPrefix + langName.replace(/=$|=\d+$|=\+$|!$|=!/, ''))
token.attrJoin('class', 'hljs')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment