refactor: extract relative url

This commit is contained in:
07akioni 2020-09-04 01:06:39 +08:00
parent b68168f238
commit 1945782401
11 changed files with 33 additions and 31 deletions

View File

@ -1,7 +1,7 @@
<template> <template>
<component-demo <component-demo
demo-file-name="<!--DEMO_FILE_NAME-->" demo-file-name="<!--DEMO_FILE_NAME-->"
demo-file-name-with-extension="<!--DEMO_FILE_NAME_WITH_EXT-->" relative-url="<!--URL-->"
title="<!--TITLE_SLOT-->" title="<!--TITLE_SLOT-->"
> >
<template v-slot:title> <template v-slot:title>

View File

@ -1,5 +1,10 @@
const convertMd2Demo = require('./convertMd2Demo') const convertMd2Demo = require('./convertMd2Demo')
const projectPath = require('./project-path')
module.exports = function (content) { module.exports = function (content) {
return convertMd2Demo(content, this.resourcePath) const relativeUrl = this.resourcePath.replace(projectPath + '/', '')
return convertMd2Demo(content, {
relativeUrl,
resourcePath: this.resourcePath
})
} }

View File

@ -1,9 +1,8 @@
const convertMd2Doc = require('./convertMd2Doc') const convertMd2Doc = require('./convertMd2Doc')
const path = require('path') const projectPath = require('./project-path')
module.exports = function (content) { module.exports = function (content) {
const env = process.env.NODE_ENV const env = process.env.NODE_ENV
const projectPath = path.resolve(__dirname, '..', '..')
const relativeURL = this.resourcePath.replace(projectPath + '/', '') const relativeURL = this.resourcePath.replace(projectPath + '/', '')
return convertMd2Doc(content, env, relativeURL) return convertMd2Doc(content, env, relativeURL)
} }

View File

@ -40,9 +40,9 @@ module.exports = function (content, url) {
.join('\n') .join('\n')
let mdContent = marked.parser(tokens, { renderer }) let mdContent = marked.parser(tokens, { renderer })
if (titleText) { if (titleText) {
const gheButton = `<edit-on-github-header url=${url || relativeURL} text=${titleText}></edit-on-github-header>` const githubButton = `<edit-on-github-header url=${url || relativeURL} text=${titleText}></edit-on-github-header>`
const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/ const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/
mdContent = mdContent.replace(titleReg, `${gheButton}`) mdContent = mdContent.replace(titleReg, `${githubButton}`)
} }
return ` return `
<template> <template>

View File

@ -72,9 +72,9 @@ ${mergedParts.code}
return mergedParts return mergedParts
} }
function genVueComponent (parts, fileName, fileNameWithExtension, noRunning = false) { function genVueComponent (parts, fileName, relativeUrl, noRunning = false) {
const demoFileNameReg = /<!--DEMO_FILE_NAME-->/g const demoFileNameReg = /<!--DEMO_FILE_NAME-->/g
const demoFileNameWithExt = /<!--DEMO_FILE_NAME_WITH_EXT-->/g const relativeUrlReg = /<!--URL-->/g
const titleReg = /<!--TITLE_SLOT-->/g const titleReg = /<!--TITLE_SLOT-->/g
const contentReg = /<!--CONTENT_SLOT-->/ const contentReg = /<!--CONTENT_SLOT-->/
const codeReg = /<!--CODE_SLOT-->/ const codeReg = /<!--CODE_SLOT-->/
@ -83,8 +83,7 @@ function genVueComponent (parts, fileName, fileNameWithExtension, noRunning = fa
const demoReg = /<!--DEMO_SLOT-->/ const demoReg = /<!--DEMO_SLOT-->/
let src = demoBlock let src = demoBlock
src = src.replace(demoFileNameReg, fileName) src = src.replace(demoFileNameReg, fileName)
src = src.replace(demoFileNameWithExt, fileNameWithExtension) src = src.replace(relativeUrlReg, relativeUrl)
// console.log(src)
if (parts.content) { if (parts.content) {
src = src.replace(contentReg, parts.content) src = src.replace(contentReg, parts.content)
} }
@ -112,13 +111,16 @@ function getFileName (resourcePath) {
return [fileNameWithExtension.split('.')[0], fileNameWithExtension] return [fileNameWithExtension.split('.')[0], fileNameWithExtension]
} }
function convertMd2Demo (text, resourcePath) { function convertMd2Demo (text, {
resourcePath,
relativeUrl
}) {
const noRunning = /<!--no-running-->/.test(text) const noRunning = /<!--no-running-->/.test(text)
const tokens = marked.lexer(text) const tokens = marked.lexer(text)
const parts = getPartsOfDemo(tokens) const parts = getPartsOfDemo(tokens)
const mergedParts = mergeParts(parts) const mergedParts = mergeParts(parts)
const [fileName, fileNameWithExtension] = getFileName(resourcePath) const [fileName] = getFileName(resourcePath)
const vueComponent = genVueComponent(mergedParts, fileName, fileNameWithExtension, noRunning) const vueComponent = genVueComponent(mergedParts, fileName, relativeUrl, noRunning)
return vueComponent return vueComponent
} }

View File

@ -127,11 +127,11 @@ function convertMd2ComponentDocumentation (text, env = 'development', url) {
const titleIndex = tokens.findIndex(token => token.type === 'heading' && token.depth === 1) const titleIndex = tokens.findIndex(token => token.type === 'heading' && token.depth === 1)
if (titleIndex > -1) { if (titleIndex > -1) {
const titleText = JSON.stringify(tokens[titleIndex].text) const titleText = JSON.stringify(tokens[titleIndex].text)
const gheButton = `<edit-on-github-header url=${url} text=${titleText}></edit-on-github-header>` const githubButton = `<edit-on-github-header relative-url="${url}" text=${titleText}></edit-on-github-header>`
const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/ const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/
documentationContent = documentationContent.replace(titleReg, `${gheButton}`) documentationContent = documentationContent.replace(titleReg, `${githubButton}`)
} }
const documentationTemplate = `s const documentationTemplate = `
<template> <template>
<component-documentation> <component-documentation>
<div style="display: flex; flex-wrap: nowrap;"> <div style="display: flex; flex-wrap: nowrap;">

View File

@ -0,0 +1,2 @@
const path = require('path')
module.exports = path.resolve(__dirname, '..', '..')

View File

@ -36,7 +36,7 @@
<edit-on-github-button <edit-on-github-button
style="padding: 0; margin-right: 6px;" style="padding: 0; margin-right: 6px;"
size="tiny" size="tiny"
:url="url" :relative-url="relativeUrl"
/> />
</template> </template>
{{ $t('editOnGithub') }} {{ $t('editOnGithub') }}
@ -90,7 +90,7 @@ export default {
type: String, type: String,
required: true required: true
}, },
demoFileNameWithExtension: { relativeUrl: {
type: String, type: String,
required: true required: true
} }
@ -113,14 +113,6 @@ export default {
computed: { computed: {
mode () { mode () {
return this.modeRef.value return this.modeRef.value
},
// TODO: make it move to loader
url () {
const relativePath = this.NDocumentation.url.replace(
'index.demo-entry.md',
this.demoFileNameWithExtension
)
return relativePath
} }
}, },
watch: { watch: {

View File

@ -14,6 +14,7 @@
<script> <script>
import editIcon from '../../src/_icons/git-commit-outline' import editIcon from '../../src/_icons/git-commit-outline'
import githubUrl from './github-url'
export default { export default {
name: 'EditOnGithubButton', name: 'EditOnGithubButton',
@ -21,7 +22,7 @@ export default {
editIcon editIcon
}, },
props: { props: {
url: { relativeUrl: {
type: String, type: String,
required: true required: true
}, },
@ -36,12 +37,12 @@ export default {
}, },
data () { data () {
return { return {
gheUrl: 'https://***REMOVED***/tree/develop/' githubUrl
} }
}, },
computed: { computed: {
gheDocUrl () { gheDocUrl () {
return this.gheUrl + this.url return this.githubUrl + this.relativeUrl
} }
}, },
methods: { methods: {

View File

@ -24,7 +24,7 @@
text text
size="large" size="large"
class="edit-button" class="edit-button"
:url="url" :relative-url="relativeUrl"
/> />
</template> </template>
{{ $t('editOnGithub') }} {{ $t('editOnGithub') }}
@ -37,7 +37,7 @@
export default { export default {
name: 'EditOnGithubHeader', name: 'EditOnGithubHeader',
props: { props: {
url: { relativeUrl: {
type: String, type: String,
required: true required: true
}, },

1
demo/utils/github-url.js Normal file
View File

@ -0,0 +1 @@
module.exports = 'https://***REMOVED***/tree/develop/'