Skip to content

Commit

Permalink
refactor: 抽离方法
Browse files Browse the repository at this point in the history
  • Loading branch information
QiuShuiBai committed Jun 18, 2024
1 parent ffc5f08 commit f9294af
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 136 deletions.
80 changes: 80 additions & 0 deletions packages/website/lib/contants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
export const markdownRenderConfig = {
props: [
{
type: 'Name',
zh: '参数',
en: 'Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
},
{
type: 'Type',
zh: '类型',
en: 'Type'
},
{
type: 'Optional',
zh: '可选值',
en: 'Optional'
},
{
type: 'Default',
zh: '默认值',
en: 'Default'
}
],
slots: [
{
type: 'Name',
zh: '插槽名',
en: 'Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
}
],
methods: [
{
type: 'Name',
zh: '组件实例方法',
en: 'Method Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
},
{
type: 'Parameters',
zh: '参数',
en: 'Parameters'
},
{
type: 'Return',
zh: '返回值',
en: 'Return'
}
],
events: [
{
type: 'Name',
zh: '事件名',
en: 'Method Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
},
{
type: 'Parameters',
zh: '参数',
en: 'Parameters'
}
]
}
161 changes: 25 additions & 136 deletions packages/website/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import path from 'path'
import fs from 'fs'
import { spawn } from 'child_process'
import { parser, type ParserResult } from '@mpxjs/vuese-parser'
import { parser } from '@mpxjs/vuese-parser'
import { Render } from '@mpxjs/vuese-markdown-render'
import { markdownRenderConfig } from './contants'

type WebsiteConfig = {
srcPath: string
Expand All @@ -29,19 +30,12 @@ function validateParams(config: WebsiteConfig) {
return true
}

function delScriptJsonBlock(content) {
function delScriptJsonBlock(content: string) {
const jsonBlockReg = /<script\s[\w\s]*(name=["']json["']|type=["']application\/json["'])(\s|[\w\s])*>[\s\S]*<\/script>/
return content.replace(jsonBlockReg, '')
}

function readFileSync(path) {
if (fs.existsSync(path)) {
return fs.readFileSync(path, 'utf-8')
}
return ''
}

const delEmptyContentLineBreaks = function (content) {
function delEmptyContentLineBreaks(content: string) {
const scriptContentReg = /(?<=<script\b[^>]*>)[\s\S]*(?=<\/script>)/ig
const scriptContent = content.match(scriptContentReg)[0]
const isEmpty = scriptContent && scriptContent.replace('\n', '').length === 0
Expand All @@ -51,7 +45,7 @@ const delEmptyContentLineBreaks = function (content) {
return content
}

function listMpxFiles(dir, fileName = '') {
function listMpxFiles(dir: string, fileName = '') {
let results: Record<'fullPath'|'fileName', string>[] = []
fs.readdirSync(dir).forEach(subFileName => {
const fullPath = path.join(dir, subFileName);
Expand All @@ -70,143 +64,38 @@ function listMpxFiles(dir, fileName = '') {
return results;
}


function getFiles(srcPath: string, examplePath: string) {
const srcFiles = listMpxFiles(srcPath)
const exapmpleFiles = fs.readdirSync(examplePath)

return srcFiles.filter(item => {
return exapmpleFiles.includes(item.fileName)
})
}

export default function website(config: WebsiteConfig): void {
if (!validateParams(config)) {
return
}

const mpxFiles = listMpxFiles(config.srcPath);

const list: {
parseResult: ParserResult,
fileName: string
}[] = []
mpxFiles.forEach(file => {
let content = readFileSync(file.fullPath)
if (!validateParams(config)) return

const srcFiles = getFiles(config.srcPath, config.examplePath)

const list = srcFiles.map(file => {
let content = fs.readFileSync(file.fullPath, 'utf-8')
content = delScriptJsonBlock(content)
content = delEmptyContentLineBreaks(content)
const res = parser(content, {
isMpx: true,
basedir: path.dirname(file.fullPath)
})
list.push({
return {
parseResult: res,
fileName: file.fileName
})
}
})

list.forEach((item, index) => {
list.forEach(item => {
// eslint-disable-next-line
// @ts-ignore
const render = new Render(item.parseResult, Object.assign({
name: index
}, {
props: [
{
type: 'Name',
zh: '参数',
en: 'Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
},
{
type: 'Type',
zh: '类型',
en: 'Type'
},
{
type: 'Optional',
zh: '可选值',
en: 'Optional'
},
{
type: 'Default',
zh: '默认值',
en: 'Default'
}
// {
// type: 'Wx',
// zh: '微信',
// en: 'WeChat'
// },
// {
// type: 'Web',
// zh: 'web',
// en: 'web'
// },
// {
// type: 'Ali',
// zh: '支付宝',
// en: 'Alipay'
// }
],
slots: [
{
type: 'Name',
zh: '插槽名',
en: 'Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
}
// {
// type: 'Default',
// zh: '默认值',
// en: 'Default Slot Content'
// }
],
methods: [
{
type: 'Name',
zh: '组件实例方法',
en: 'Method Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
},
{
type: 'Parameters',
zh: '参数',
en: 'Parameters'
},
{
type: 'Return',
zh: '返回值',
en: 'Return'
}
],
events: [
{
type: 'Name',
zh: '事件名',
en: 'Method Name'
},
{
type: 'Description',
zh: '说明',
en: 'Description'
},
{
type: 'Parameters',
zh: '参数',
en: 'Parameters'
}
],
mixIns: [
{
type: 'Name',
zh: '参数',
en: 'Name'
}
]
}))
const render = new Render(item.parseResult, Object.assign({ name: item.fileName }, markdownRenderConfig))
fs.writeFileSync(`${config.outputPath}/${item.fileName}.md`, render.renderMarkdown('zh')!.content)
})

Expand Down

0 comments on commit f9294af

Please sign in to comment.