Skip to content

Commit

Permalink
feat: move eslint config to antfu config
Browse files Browse the repository at this point in the history
  • Loading branch information
07akioni committed Jul 11, 2024
1 parent cb9b659 commit 1dfb1be
Show file tree
Hide file tree
Showing 2,049 changed files with 33,952 additions and 31,437 deletions.
10 changes: 5 additions & 5 deletions build/loaders/ComponentDemoTemplate.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
language-type="<!--LANGUAGE_TYPE_SLOT-->"
>
<template #title>
<!--TITLE_SLOT-->
<!-- TITLE_SLOT -->
</template>
<template #content>
<!--CONTENT_SLOT-->
<!-- CONTENT_SLOT -->
</template>
<template #demo>
<div class="demo-card__view">
<!--DEMO_SLOT-->
<!-- DEMO_SLOT -->
</div>
</template>
</component-demo>
</template>

<!--SCRIPT_SLOT-->
<!-- SCRIPT_SLOT -->

<!--STYLE_SLOT-->
<!-- STYLE_SLOT -->
56 changes: 32 additions & 24 deletions build/loaders/convert-md-to-demo.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const fs = require('node:fs')
const path = require('node:path')
const { marked } = require('marked')
const fs = require('fs')
const path = require('path')
const createRenderer = require('./md-renderer')
const handleMergeCode = require('../utils/handle-merge-code.js')
const createRenderer = require('./md-renderer')

const mdRenderer = createRenderer()

const __HTTP__ = process.env.NODE_ENV !== 'production' ? 'http' : 'https'
const __HTTP__
= require('node:process').env.NODE_ENV !== 'production' ? 'http' : 'https'

const demoBlock = fs
.readFileSync(path.resolve(__dirname, 'ComponentDemoTemplate.vue'))
.toString()

function getPartsOfDemo (tokens) {
function getPartsOfDemo(tokens) {
let template = null
let script = null
let style = null
Expand All @@ -22,23 +24,27 @@ function getPartsOfDemo (tokens) {
for (const token of tokens) {
if (token.type === 'heading' && token.depth === 1) {
title = token.text
} else if (
token.type === 'code' &&
(token.lang === 'template' || token.lang === 'html')
}
else if (
token.type === 'code'
&& (token.lang === 'template' || token.lang === 'html')
) {
template = token.text
} else if (
token.type === 'code' &&
(token.lang === 'script' || token.lang === 'js' || token.lang === 'ts')
}
else if (
token.type === 'code'
&& (token.lang === 'script' || token.lang === 'js' || token.lang === 'ts')
) {
languageType = token.lang
script = token.text
} else if (
token.type === 'code' &&
(token.lang === 'style' || token.lang === 'css')
}
else if (
token.type === 'code'
&& (token.lang === 'style' || token.lang === 'css')
) {
style = token.text
} else {
}
else {
contentTokens.push(token)
}
}
Expand All @@ -54,7 +60,7 @@ function getPartsOfDemo (tokens) {
}
}

function mergeParts ({ parts, isVue }) {
function mergeParts({ parts, isVue }) {
const mergedParts = {
...parts
}
Expand All @@ -75,27 +81,29 @@ const cssRuleRegex = /([^{}]*)(\{[^}]*\})/g
// xxx {
// mystyle
// }
function genStyle (sourceStyle) {
function genStyle(sourceStyle) {
let match
let matched = false
const rules = []

// eslint-disable-next-line no-cond-assign
while ((match = cssRuleRegex.exec(sourceStyle)) !== null) {
matched = true
const selector = match[1]
const body = match[2]
rules.push(
selector
.split(',')
.map((part) => `.demo-card__view ${part}, .naive-ui-doc ${part}`)
.map(part => `.demo-card__view ${part}, .naive-ui-doc ${part}`)
.join(',') + body
)
}
if (!matched) return null
return '<style scoped>\n' + rules.join('\n') + '</style>'
if (!matched)
return null
return `<style scoped>\n${rules.join('\n')}</style>`
}

function genVueComponent (parts, fileName, relativeUrl) {
function genVueComponent(parts, fileName, relativeUrl) {
const demoFileNameReg = /<!--DEMO_FILE_NAME-->/g
const relativeUrlReg = /<!--URL-->/g
const titleReg = /<!--TITLE_SLOT-->/g
Expand Down Expand Up @@ -126,7 +134,7 @@ function genVueComponent (parts, fileName, relativeUrl) {
parts.language === 'ts' ? ' lang="ts"' : ''
}`
const startScriptTag = `<script${attributes}>\n`
src = src.replace(scriptReg, startScriptTag + parts.script + '\n</script>')
src = src.replace(scriptReg, `${startScriptTag + parts.script}\n</script>`)
}
if (parts.language) {
src = src.replace(languageTypeReg, parts.language)
Expand All @@ -146,13 +154,13 @@ function genVueComponent (parts, fileName, relativeUrl) {
return src.trim()
}

function getFileName (resourcePath) {
function getFileName(resourcePath) {
const dirs = resourcePath.split('/')
const fileNameWithExtension = dirs[dirs.length - 1]
return [fileNameWithExtension.split('.')[0], fileNameWithExtension]
}

function convertMd2Demo (text, { resourcePath, relativeUrl, isVue = false }) {
function convertMd2Demo(text, { resourcePath, relativeUrl, isVue = false }) {
const tokens = marked.lexer(text)
const parts = getPartsOfDemo(tokens)
const mergedParts = mergeParts({ parts, isVue })
Expand Down
52 changes: 28 additions & 24 deletions build/loaders/convert-md-to-doc.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
const path = require('path')
const path = require('node:path')
const fse = require('fs-extra')
const { marked } = require('marked')
const camelCase = require('lodash/camelCase')
const createRenderer = require('./md-renderer')
const projectPath = require('./project-path')

const mdRenderer = createRenderer()

async function resolveDemoTitle (fileName, demoEntryPath) {
async function resolveDemoTitle(fileName, demoEntryPath) {
const demoStr = await fse.readFile(
path.resolve(projectPath, demoEntryPath, '..', fileName),
'utf-8'
)
return demoStr.match(/# ([^\n]+)/)[1]
}

async function resolveDemoInfos (literal, url, env) {
async function resolveDemoInfos(literal, url, env) {
const ids = literal
.split('\n')
.map((line) => line.trim())
.filter((id) => id.length)
.map(line => line.trim())
.filter(id => id.length)
const infos = []
for (const id of ids) {
const debug = id.includes('debug') || id.includes('Debug')
Expand All @@ -27,8 +28,9 @@ async function resolveDemoInfos (literal, url, env) {
}
let fileName
if (id.includes('.vue')) {
fileName = id.slice(0, -4) + '.demo.vue'
} else {
fileName = `${id.slice(0, -4)}.demo.vue`
}
else {
fileName = `${id}.demo.md`
}
const variable = `${camelCase(id)}Demo`
Expand All @@ -44,13 +46,13 @@ async function resolveDemoInfos (literal, url, env) {
return infos
}

function genDemosTemplate (demoInfos, colSpan) {
function genDemosTemplate(demoInfos, colSpan) {
return `<component-demos :span="${colSpan}">${demoInfos
.map(({ tag }) => tag)
.join('\n')}</component-demos>`
}

function genAnchorTemplate (
function genAnchorTemplate(
children,
options = {
ignoreGap: false
Expand All @@ -70,7 +72,7 @@ function genAnchorTemplate (
`
}

function genDemosApiAnchorTemplate (tokens) {
function genDemosApiAnchorTemplate(tokens) {
const api = [
{
id: 'API',
Expand All @@ -80,16 +82,16 @@ function genDemosApiAnchorTemplate (tokens) {
]
return api.concat(
tokens
.filter((token) => token.type === 'heading' && token.depth === 3)
.map((token) => ({
.filter(token => token.type === 'heading' && token.depth === 3)
.map(token => ({
id: token.text.replace(/ /g, '-'),
title: token.text,
debug: false
}))
)
}

function genDemosAnchorTemplate (demoInfos, hasApi, tokens) {
function genDemosAnchorTemplate(demoInfos, hasApi, tokens) {
const links = (
hasApi ? demoInfos.concat(genDemosApiAnchorTemplate(tokens)) : demoInfos
).map(
Expand All @@ -104,18 +106,18 @@ function genDemosAnchorTemplate (demoInfos, hasApi, tokens) {
})
}

function genPageAnchorTemplate (tokens) {
function genPageAnchorTemplate(tokens) {
const titles = tokens
.filter((token) => token.type === 'heading' && token.depth === 2)
.map((token) => token.text)
.filter(token => token.type === 'heading' && token.depth === 2)
.map(token => token.text)
const links = titles.map((title) => {
const href = title.replace(/ /g, '-')
return `<n-anchor-link title="${title}" href="#${href}"/>`
})
return genAnchorTemplate(links.join('\n'), { ignoreGap: true })
}

function genScript (demoInfos, components = [], url, forceShowAnchor) {
function genScript(demoInfos, components = [], url, forceShowAnchor) {
const showAnchor = !!(demoInfos.length || forceShowAnchor)
const importStmts = demoInfos
.map(({ variable, fileName }) => `import ${variable} from './${fileName}'`)
Expand Down Expand Up @@ -164,7 +166,7 @@ export default {
return script
}

async function convertMd2ComponentDocumentation (
async function convertMd2ComponentDocumentation(
text,
url,
env = 'development'
Expand All @@ -175,7 +177,7 @@ async function convertMd2ComponentDocumentation (
const tokens = marked.lexer(text)
// resolve external components
const componentsIndex = tokens.findIndex(
(token) => token.type === 'code' && token.lang === 'component'
token => token.type === 'code' && token.lang === 'component'
)
let components = []
if (~componentsIndex) {
Expand All @@ -184,10 +186,12 @@ async function convertMd2ComponentDocumentation (
.split('\n')
.map((component) => {
const [ids, importStmt] = component.split(':')
if (!ids.trim()) throw new Error('No component id')
if (!importStmt.trim()) throw new Error('No component source url')
if (!ids.trim())
throw new Error('No component id')
if (!importStmt.trim())
throw new Error('No component source url')
return {
ids: ids.split(',').map((id) => id.trim()),
ids: ids.split(',').map(id => id.trim()),
importStmt: importStmt.trim()
}
})
Expand All @@ -196,7 +200,7 @@ async function convertMd2ComponentDocumentation (
}
// add edit on github button on title
const titleIndex = tokens.findIndex(
(token) => token.type === 'heading' && token.depth === 1
token => token.type === 'heading' && token.depth === 1
)
if (titleIndex > -1) {
const titleText = JSON.stringify(tokens[titleIndex].text)
Expand All @@ -209,7 +213,7 @@ async function convertMd2ComponentDocumentation (
}
// resolve demos, debug demos are removed from production build
const demosIndex = tokens.findIndex(
(token) => token.type === 'code' && token.lang === 'demo'
token => token.type === 'code' && token.lang === 'demo'
)
let demoInfos = []
if (~demosIndex) {
Expand Down
10 changes: 6 additions & 4 deletions build/loaders/convert-vue-to-demo.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const { marked } = require('marked')
const createRenderer = require('./md-renderer')

const mdRenderer = createRenderer()
const {
genVueComponent,
getFileName,
mergeParts
} = require('./convert-md-to-demo')

function getPartsOfDemo (text) {
function getPartsOfDemo(text) {
// slot template
const firstIndex = text.indexOf('<template>')
let template = text.slice(firstIndex + 10)
Expand All @@ -24,7 +25,8 @@ function getPartsOfDemo (text) {
for (const token of tokens) {
if (token.type === 'heading' && token.depth === 1) {
title = token.text
} else {
}
else {
contentTokens.push(token)
}
}
Expand All @@ -48,13 +50,13 @@ function getPartsOfDemo (text) {
}
}

function convertVue2Demo (content, { resourcePath, relativeUrl, isVue = true }) {
function convertVue2Demo(content, { resourcePath, relativeUrl, isVue = true }) {
const parts = getPartsOfDemo(content)
const mergedParts = mergeParts({ parts, isVue })
const [fileName] = getFileName(resourcePath)
const vueComponent = genVueComponent(
mergedParts,
fileName + '.vue',
`${fileName}.vue`,
relativeUrl
)
return vueComponent
Expand Down
Loading

0 comments on commit 1dfb1be

Please sign in to comment.