modify(EditOnGithubButton)

This commit is contained in:
songwanli2025@163.com 2020-03-19 13:26:06 +08:00
parent 40a1e44036
commit d6b0603177
9 changed files with 150 additions and 94 deletions

View File

@ -80,6 +80,8 @@ import ComponentDemo from './utils/ComponentDemo'
import ComponentDemos from './utils/ComponentDemos' import ComponentDemos from './utils/ComponentDemos'
import ComponentDocumentation from './utils/ComponentDocumentation' import ComponentDocumentation from './utils/ComponentDocumentation'
import DocumentationWrapper from './utils/DocumentationWrapper' import DocumentationWrapper from './utils/DocumentationWrapper'
import EditOnGithubButton from './utils/EditOnGithubButton'
import EditOnGithubHeader from './utils/EditOnGithubHeader'
import './styles/demo.scss' import './styles/demo.scss'
Vue.use(VueRouter) Vue.use(VueRouter)
@ -88,6 +90,8 @@ Vue.component('ComponentDemo', ComponentDemo)
Vue.component('ComponentDemos', ComponentDemos) Vue.component('ComponentDemos', ComponentDemos)
Vue.component('DocumentationWrapper', DocumentationWrapper) Vue.component('DocumentationWrapper', DocumentationWrapper)
Vue.component('ComponentDocumentation', ComponentDocumentation) Vue.component('ComponentDocumentation', ComponentDocumentation)
Vue.component('EditOnGithubButton', EditOnGithubButton)
Vue.component('EditOnGithubHeader', EditOnGithubHeader)
const withPrefix = (prefix, routes) => const withPrefix = (prefix, routes) =>
routes.map(route => { routes.map(route => {

View File

@ -81,6 +81,8 @@ import ComponentDemos from './utils/ComponentDemos'
import ComponentDocumentation from './utils/ComponentDocumentation' import ComponentDocumentation from './utils/ComponentDocumentation'
import DocumentationWrapper from './utils/DocumentationWrapper' import DocumentationWrapper from './utils/DocumentationWrapper'
import './styles/demo.scss' import './styles/demo.scss'
import EditOnGithubButton from './utils/EditOnGithubButton'
import EditOnGithubHeader from './utils/EditOnGithubHeader'
import routerDebug from './debugComponents/routerDebug' import routerDebug from './debugComponents/routerDebug'
import modalDebug from './debugComponents/modalDebug' import modalDebug from './debugComponents/modalDebug'
@ -101,6 +103,8 @@ Vue.component('ComponentDemo', ComponentDemo)
Vue.component('ComponentDemos', ComponentDemos) Vue.component('ComponentDemos', ComponentDemos)
Vue.component('DocumentationWrapper', DocumentationWrapper) Vue.component('DocumentationWrapper', DocumentationWrapper)
Vue.component('ComponentDocumentation', ComponentDocumentation) Vue.component('ComponentDocumentation', ComponentDocumentation)
Vue.component('EditOnGithubButton', EditOnGithubButton)
Vue.component('EditOnGithubHeader', EditOnGithubHeader)
const withPrefix = (prefix, routes) => const withPrefix = (prefix, routes) =>
routes.map(route => { routes.map(route => {

View File

@ -13,7 +13,13 @@
height:500px; height:500px;
border:1px solid rgba(0, 0, 0, .1); border:1px solid rgba(0, 0, 0, .1);
margin-top: 10px;"> margin-top: 10px;">
<p style="position:absolute;top:50%;left:50%;transform: translate(-50%, -50%);">Appear area</p> <p style="
position:absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);">
Appear area
</p>
</div> </div>
<n-drawer v-model="active" :width="200" :placement="placement" :target="target"> <n-drawer v-model="active" :width="200" :placement="placement" :target="target">
<n-h1>Stoner</n-h1> <n-h1>Stoner</n-h1>

View File

@ -14,7 +14,12 @@
border:1px solid rgba(0, 0, 0, .1); border:1px solid rgba(0, 0, 0, .1);
margin-top: 10px;" margin-top: 10px;"
> >
<p style="position:absolute;top:50%;left:50%;transform: translate(-50%, -50%);">显示区域</p> <p style="position:absolute;
top:50%;
left:50%;
transform: translate(-50%, -50%);">
显示区域
</p>
</div> </div>
<n-drawer v-model="active" :width="200" :placement="placement" :target="target"> <n-drawer v-model="active" :width="200" :placement="placement" :target="target">
<n-h1>斯通纳</n-h1> <n-h1>斯通纳</n-h1>

View File

@ -22,30 +22,27 @@ function parseComponents (tokens) {
return components return components
} }
module.exports = function (content, titleReg, gheUrl, gheButton) { module.exports = function (content, url) {
const tokens = marked.lexer(content) const tokens = marked.lexer(content)
const titleIndex = tokens.findIndex(token => token.type === 'heading' && token.depth === 1)
let titleText = titleIndex > -1 ? JSON.stringify(tokens[titleIndex].text) : null
const anchor = parseMdAsAnchor(tokens) const anchor = parseMdAsAnchor(tokens)
const components = parseComponents(tokens) const components = parseComponents(tokens)
const importStatements = components const importStatements = components
.map(component => `import ${component} from './${component}'`) .map(component => `import ${component} from './${component}'`)
.join('\n') .join('\n')
const mdContent = marked.parser(tokens, { renderer }) let mdContent = marked.parser(tokens, { renderer })
const documentationContent = mdContent.replace(titleReg, `$1$2${gheButton}$3`) if (titleText) {
return `<i18n> const gheButton = `<edit-on-github-header url=${url} text=${titleText}></edit-on-github-header>`
{ const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/
"zh-CN": { mdContent = mdContent.replace(titleReg, `${gheButton}`)
"editOnGithub": "在 Github 上编辑"
},
"en-US": {
"editOnGithub": "Edit on Github"
}
} }
</i18n> return `
<template> <template>
<component-documentation> <component-documentation>
<div style="display: flex; flex-wrap: nowrap;"> <div style="display: flex; flex-wrap: nowrap;">
<div style="width: calc(100% - 180px); margin-right: 36px;"> <div style="width: calc(100% - 180px); margin-right: 36px;">
${documentationContent} ${mdContent}
</div> </div>
<div style="width: 144px;"> <div style="width: 144px;">
${anchor} ${anchor}
@ -55,23 +52,10 @@ module.exports = function (content, titleReg, gheUrl, gheButton) {
</template> </template>
<script> <script>
${importStatements} ${importStatements}
import createOutline from 'naive-ui/lib/icons/create-outline'
export default { export default {
components: { components: {
createOutline,
${components.join(',\n')} ${components.join(',\n')}
},
data () {
return {
gheUrl: ${JSON.stringify(gheUrl)}
}
},
methods: {
handleEditOnGithubClick () {
window.open(this.gheUrl, '_blank')
return false
},
} }
} }
</script>` </script>`

View File

@ -46,7 +46,7 @@ function parseDemosAsAnchor (demosLiteral) {
return `<n-anchor :top="32" :bound="16" position="absolute" affix style="width: 144px;">${linkTags.join('\n')}</n-anchor>` return `<n-anchor :top="32" :bound="16" position="absolute" affix style="width: 144px;">${linkTags.join('\n')}</n-anchor>`
} }
function generateScript (demosLiteral, components = [], gheUrl) { function generateScript (demosLiteral, components = [], url) {
const demoNames = demosLiteral const demoNames = demosLiteral
.split('\n') .split('\n')
.map(demoName => demoName.trim()) .map(demoName => demoName.trim())
@ -60,11 +60,9 @@ function generateScript (demosLiteral, components = [], gheUrl) {
const componentStatements = demoNames.map(demoName => demoName + 'Demo').concat(components).join(', ') const componentStatements = demoNames.map(demoName => demoName + 'Demo').concat(components).join(', ')
const script = `<script> const script = `<script>
${importStatements} ${importStatements}
import createOutline from 'naive-ui/lib/icons/create-outline'
export default { export default {
components: { components: {
createOutline,
${componentStatements} ${componentStatements}
}, },
provide () { provide () {
@ -75,14 +73,8 @@ export default {
data () { data () {
return { return {
anchorLinkMap: new Map(), anchorLinkMap: new Map(),
gheUrl: ${JSON.stringify(gheUrl)} url: ${JSON.stringify(url)}
} }
},
methods: {
handleEditOnGithubClick () {
window.open(this.gheUrl, '_blank')
return false
},
} }
} }
</script>` </script>`
@ -91,32 +83,8 @@ export default {
function convertMd2ComponentDocumentation (text, env = 'development', url) { function convertMd2ComponentDocumentation (text, env = 'development', url) {
const isNoDemo = !!~text.search('<!--no-demo-->') const isNoDemo = !!~text.search('<!--no-demo-->')
const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/
const gheUrl = 'https://***REMOVED***/tree/develop/' + url
const gheButton = `<n-tooltip
:delay="300"
:placement="'top'"
:show-arrow="true"
>
<template v-slot:activator>
<a href="">
<n-button
style="marginLeft:10px;verticalAlign:middle;"
ghost
round
@click="handleEditOnGithubClick"
size="small"
>
<template v-slot:icon>
<create-outline />
</template>
</n-button>
</a>
</template>
{{ $t('editOnGithub') }}
</n-tooltip>`
if (isNoDemo) { if (isNoDemo) {
return mdLoader(text, titleReg, gheUrl, gheButton) return mdLoader(text, url)
} }
const isSingleColumn = !!~text.search('<!--single-column-->') const isSingleColumn = !!~text.search('<!--single-column-->')
const tokens = marked.lexer(text) const tokens = marked.lexer(text)
@ -157,20 +125,16 @@ function convertMd2ComponentDocumentation (text, env = 'development', url) {
// const classedDocumentationHTML = addClassToHTML(documentationHTML, 'markdown') // const classedDocumentationHTML = addClassToHTML(documentationHTML, 'markdown')
const demosReg = /<!--demos-->/ const demosReg = /<!--demos-->/
const demoTags = parseDemos(demosLiteral, env) const demoTags = parseDemos(demosLiteral, env)
const documentationContent = documentationHTML let documentationContent = documentationHTML
.replace(demosReg, template(demoTags, demosLiteral, isSingleColumn)) .replace(demosReg, template(demoTags, demosLiteral, isSingleColumn))
.replace(titleReg, `$1$2${gheButton}$3`) const titleIndex = tokens.findIndex(token => token.type === 'heading' && token.depth === 1)
const documentationTemplate = ` if (titleIndex > -1) {
<i18n> const titleText = JSON.stringify(tokens[titleIndex].text)
{ const gheButton = `<edit-on-github-header url=${url} text=${titleText}></edit-on-github-header>`
"zh-CN": { const titleReg = /(<n-h1[^>]*>)(.*?)(<\/n-h1>)/
"editOnGithub": "在 Github 上编辑" documentationContent = documentationContent.replace(titleReg, `${gheButton}`)
},
"en-US": {
"editOnGithub": "Edit on Github"
}
} }
</i18n> const documentationTemplate = `s
<template> <template>
<component-documentation> <component-documentation>
<div style="display: flex; flex-wrap: nowrap;"> <div style="display: flex; flex-wrap: nowrap;">
@ -183,7 +147,7 @@ function convertMd2ComponentDocumentation (text, env = 'development', url) {
</div> </div>
</component-documentation> </component-documentation>
</template>` </template>`
const documentationScript = generateScript(demosLiteral, components, gheUrl) const documentationScript = generateScript(demosLiteral, components, url)
// if (components.length) console.log(`${documentationTemplate}\n\n${documentationScript}`) // if (components.length) console.log(`${documentationTemplate}\n\n${documentationScript}`)
return `${documentationTemplate}\n\n${documentationScript}` return `${documentationTemplate}\n\n${documentationScript}`
// console.log(vueComponent) // console.log(vueComponent)

View File

@ -34,16 +34,10 @@
:show-arrow="true" :show-arrow="true"
> >
<template v-slot:activator> <template v-slot:activator>
<n-button <edit-on-github-button
style="padding: 0 2px 0 8px" class="edit-button"
ghost :url="url"
round />
@click="handleEditOnGithubClick"
>
<template v-slot:icon>
<create-outline />
</template>
</n-button>
</template> </template>
{{ $t('editOnGithub') }} {{ $t('editOnGithub') }}
</n-tooltip> </n-tooltip>
@ -81,14 +75,12 @@
<script> <script>
import codeOutline from '../../src/_icons/code-outline' import codeOutline from '../../src/_icons/code-outline'
import createOutline from '../../src/_icons/create-outline'
import { state } from '../store' import { state } from '../store'
import camelCase from 'lodash/camelCase' import camelCase from 'lodash/camelCase'
export default { export default {
components: { components: {
codeOutline, codeOutline
createOutline
}, },
inject: { inject: {
NDocumentation: { NDocumentation: {
@ -111,7 +103,7 @@ export default {
return this.state.mode return this.state.mode
}, },
url () { url () {
const relativePath = this.NDocumentation.gheUrl.replace('index.md', camelCase(this.name) + '.md') const relativePath = this.NDocumentation.url.replace('index.md', camelCase(this.name) + '.md')
return relativePath return relativePath
} }
}, },
@ -134,10 +126,6 @@ export default {
this.init() this.init()
}, },
methods: { methods: {
handleEditOnGithubClick () {
window.open(this.url, '_blank')
return false
},
toggleCodeDisplay () { toggleCodeDisplay () {
this.showCode = !this.showCode this.showCode = !this.showCode
}, },

View File

@ -0,0 +1,47 @@
<template>
<n-button
class="edit-button"
ghost
round
@click="handleEditOnGithubClick"
>
<template v-slot:icon>
<create-outline />
</template>
</n-button>
</template>
<script>
import createOutline from '../../src/_icons/create-outline'
export default {
name: 'EditOnGithubButton',
components: {
createOutline
},
props: {
url: {
type: String,
required: true
}
},
data () {
return {
gheUrl: 'https://***REMOVED***/tree/develop/'
}
},
computed: {
gheDocUrl () {
return this.gheUrl + this.url
}
},
methods: {
handleEditOnGithubClick () {
return window.open(this.gheDocUrl, '_blank')
}
}
}
</script>
<style lang="scss" scoped>
.edit-button {
vertical-align: middle;
}
</style>

View File

@ -0,0 +1,54 @@
<i18n>
{
"zh-CN": {
"editOnGithub": "在 Github 上编辑"
},
"en-US": {
"editOnGithub": "Edit on Github"
}
}
</i18n>
<template>
<n-h1 :id="id">
{{ text }}
<n-tooltip
:delay="300"
:placement="'top'"
:show-arrow="true"
>
<template v-slot:activator>
<edit-on-github-button
class="edit-button"
:url="url"
/>
</template>
{{ $t('editOnGithub') }}
</n-tooltip>
</n-h1>
</template>
<script>
export default {
name: 'EditOnGithubHeader',
props: {
url: {
type: String,
required: true
},
text: {
type: String,
required: true
}
},
computed: {
id () {
return this.text.replace(/ /g, '-')
}
}
}
</script>
<style lang="scss" scoped>
.edit-button {
margin-left: 10px;
}
</style>