Skip to content

Commit

Permalink
Merge pull request #15 from tolking/dev
Browse files Browse the repository at this point in the history
refactor: add reVeturDescription and slotsSubtags
  • Loading branch information
tolking authored May 1, 2021
2 parents e22a198 + ec35e03 commit 9cc4305
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 58 deletions.
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ rewriting the doc url of the component

rewriting the attribute of the component

### reVeturDescription

- Type: `(description?: string, defaultValue?: string, docUrl?: string) => string`
- Default: same like `${description}, default: ${defaultValue}.\n\n[Docs](${docUrl})`

rewriting the description of vetur

### space

- Type: `number` | `string`
Expand Down Expand Up @@ -119,6 +126,13 @@ name for attributes of the vetur

name for web-types of the webstrom

### separator

- Type: `string`
- Default: `/`

the separator for propsOptions and slotsSubtags

### props

- Type: `string` (**This is a regular string and ignores case.**)
Expand Down Expand Up @@ -154,13 +168,6 @@ name for props header type

name for props header options

### separator

- Type: `string`
- Default: `/`

name for props options separator

### propsDefault

- Type: `string`
Expand Down Expand Up @@ -210,6 +217,13 @@ name for slots header name

name for slots header description

### slotsSubtags

- Type: `string`
- Default: `Subtags`

name for slots header subtags

### directives

- Type: `string` (**This is a regular string and ignores case.**)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "components-helper",
"version": "1.0.2",
"version": "1.0.3",
"description": "Based on the docs to provide code prompt files for vue component library",
"main": "lib/index.js",
"module": "lib/index.es.js",
Expand Down
3 changes: 1 addition & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ const config: Config = {
propsDescription: 'Description',
propsOptions: 'Options',
propsDefault: 'Default',
defaultValSeparators: [', ', '.'],
separator: '/',
events: 'events',
eventsName: 'Name',
eventsDescription: 'Description',
slots: 'slots',
slotsName: 'Name',
slotsDescription: 'Description',
slotsSubtags: 'Subtags',
directives: 'directives',
directivesName: 'Name',
directivesType: 'Type',
directivesDescription: 'Description',
subtagsMap: {},
}

export default config
14 changes: 8 additions & 6 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ type ReAttribute = (
title: string,
) => string | undefined

type ReVeturDescription = (
description?: string,
defaultValue?: string,
docUrl?: string,
) => string

interface OptionsConfig {
entry: string
outDir: string
Expand All @@ -18,6 +24,7 @@ interface OptionsConfig {
reComponentName?: ReComponentName
reDocUrl?: ReDocUrl
reAttribute?: ReAttribute
reVeturDescription?: ReVeturDescription
}

export interface Config {
Expand All @@ -33,19 +40,18 @@ export interface Config {
propsDescription: string
propsOptions: string
propsDefault: string
defaultValSeparators: [string, string]
separator: string
events: string
eventsName: string
eventsDescription: string
slots: string
slotsName: string
slotsDescription: string
slotsSubtags: string
directives: string
directivesName: string
directivesType: string
directivesDescription: string
subtagsMap: SubTagsMap
}

export type InstallOptions = OptionsConfig & Partial<Config>
Expand Down Expand Up @@ -153,7 +159,3 @@ export interface WebTypes {
}
}
}

export interface SubTagsMap {
[propName: string]: string[]
}
72 changes: 39 additions & 33 deletions src/vetur.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getComponentsName, getDocUrl, checkArray } from './utils'
import { getComponentsName, getDocUrl, checkArray, isFunction } from './utils'
import type { Options, NormalizeData, Tags, Props } from './type'

function vetur(
Expand All @@ -17,6 +17,8 @@ function vetur(
separator,
eventsName,
eventsDescription,
slotsSubtags,
reVeturDescription,
} = options
const tagsList = {} as Tags
const propsList = {} as Props
Expand All @@ -35,7 +37,9 @@ function vetur(
const name = getComponentsName(options, title, fileName, path)
const _props = props ? props.content : []
const _events = events ? events.content : []
const _slots = slots ? slots.content : []
const tagsProps: string[] = []
let subtags: string[] = []

if (children && children.length) {
const { tags, attributes } = vetur(options, children)
Expand All @@ -49,26 +53,27 @@ function vetur(
const _item = item[propsName]

if (_item) {
const docUrl = getDocUrl(options, fileName, props?.title, path)
const _name = name + '/' + _item
const _type = item[propsType] || ''
const _options = item[propsOptions]
const _optionsList =
/string/i.test(_type) && _options
? _options.split(separator).map((item) => item.trim())
: undefined
const _description = isFunction(reVeturDescription)
? reVeturDescription(
item[propsDescription],
item[propsDefault],
docUrl,
)
: reDescription(item[propsDescription], item[propsDefault], docUrl)

tagsProps.push(_item)
propsList[_name] = {
type: item[propsType],
options: _optionsList,
description: reDescription(
options,
fileName,
item[propsDescription],
props?.title,
item[propsDefault],
path,
),
description: _description,
}
}
})
Expand All @@ -77,54 +82,55 @@ function vetur(
const _item = item[eventsName]

if (_item) {
const docUrl = getDocUrl(options, fileName, events?.title, path)
const _name = name + '/' + _item
const _description = isFunction(reVeturDescription)
? reVeturDescription(item[eventsDescription], undefined, docUrl)
: reDescription(item[eventsDescription], undefined, docUrl)

tagsProps.push(_item)
propsList[_name] = {
type: 'event',
description: reDescription(
options,
fileName,
item[eventsDescription],
events?.title,
item[propsDefault],
path,
),
description: _description,
}
}
})

_slots.forEach((item) => {
const _subtags = item[slotsSubtags]
const _subtagsList = _subtags
? _subtags.split(separator).map((item) => item.trim())
: undefined

if (_subtagsList) {
subtags = subtags.concat(_subtagsList)
}
})

const docUrl = getDocUrl(options, fileName, events?.title, path)
const _description = isFunction(reVeturDescription)
? reVeturDescription(description, undefined, docUrl)
: reDescription(description, undefined, docUrl)

tagsList[name] = {
attributes: checkArray(tagsProps),
subtags: options.subtagsMap[name],
description: reDescription(
options,
fileName,
description,
title,
undefined,
path,
),
subtags: checkArray(subtags),
description: _description,
}
}

return { tags: tagsList, attributes: propsList }
}

function reDescription(
options: Options,
fileName: string,
description?: string,
header?: string,
defaultVal?: string,
path?: string,
docUrl?: string,
): string | undefined {
const docUrl = getDocUrl(options, fileName, header, path)
let str = description || ''
const separators = options.defaultValSeparators

if (defaultVal) {
str += `${str ? separators[0] : ''}default: ${defaultVal}${separators[1]}`
str += `${str ? ', ' : ''}default: ${defaultVal}.`
}
if (docUrl) {
str += `${str ? '\n\n' : ''}[Docs](${docUrl})`
Expand Down
7 changes: 4 additions & 3 deletions test/button.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Display multiple buttons as button groups

### ButtonGroup Slots

| Name | Description |
| ------ | ------------------ |
| default | customize button group content |
| Name | Description | Subtags |
| ------ | ------------------ | -- |
| default | customize button group content | Button / Test |
| other | some other slots ||
16 changes: 10 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const helper = require('../lib/index')
const subtagsMap = {
'app-button-group': ['app-button'],
}

helper({
name: 'test',
Expand All @@ -12,7 +9,7 @@ helper({
reComponentName,
reDocUrl,
reAttribute,
subtagsMap,
space: 2,
props: 'Attributes',
propsName: 'Attribute',
propsOptions: 'Accepted Values',
Expand All @@ -29,7 +26,7 @@ function reDocUrl(fileName, header) {
return docs + fileName + (_header ? '#' + header : '')
}

function reAttribute(str) {
function reAttribute(str, key) {
switch (str) {
case '':
case '-':
Expand All @@ -38,6 +35,13 @@ function reAttribute(str) {
case 'v-model':
return 'model-value'
default:
return str
if (key === 'Subtags') {
return str
.split('/')
.map((name) => reComponentName(name.trim()))
.join('/')
} else {
return str
}
}
}

0 comments on commit 9cc4305

Please sign in to comment.