Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/website-build/src/compiler/compile-src-code-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@ import { parser } from '@mpxjs/vuese-parser'
import { Render } from '@mpxjs/vuese-markdown-render'
import { renderOptions } from '../common/options.js'

/**
* 将字符串的对象转为可读的markdown格式
* @param str
* @returns
*/
function convertObjectStringToMarkdownHtml(str) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果某个属性也是对象的话,缩进会有缺陷。可以看看优化下

a = {
    b: {
        c: 1
    }
    d: 2
}

if (!str) return
return (
'<pre><code>' +
str
.split('\n')
.map((line, idx) => {
line = line.replace(/\s+/g, '').replace(/</g, '&lt;').replace(/>/g, '&gt;')
if (idx !== 0 && idx !== str.split('\n').length - 1) {
line = ' ' + line
}
return line
}) // 防止HTML标签注入
.join('<br>') +
'</code></pre>'
)
}

/**
* 解析 cube-ui src 文件夹下的组件,生成md文档
*
Expand Down Expand Up @@ -44,6 +67,9 @@ const genSrcCodeMd = function (srcDir: string, component: string, fnMixins) {
item.type = item.type.replace(/\n/g, '<br>')
item.type = item.type.replace(new RegExp(optionalMark, 'g'), '?')
})
text.props?.forEach(item => {
item.default = convertObjectStringToMarkdownHtml(item.default)
})
const render = new Render(text, Object.assign({
name: component
} as any, renderOptions))
Expand Down