Skip to content

Commit

Permalink
Merge pull request #17 from tolking/dev
Browse files Browse the repository at this point in the history
feat: add reWebTypesSource
  • Loading branch information
tolking authored Aug 14, 2021
2 parents 4cc02ef + f339fe8 commit 61cf40b
Show file tree
Hide file tree
Showing 14 changed files with 323 additions and 169 deletions.
43 changes: 43 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Changelog

### 1.0.4

feat

- add `reWebTypesSource` config

fix

- the header error of description in tags

### 1.0.3

feat

- add `reVeturDescription` config
- add arg for reDocUrl (path: string)
- resolve `Subtags` in header of tabel
- parsing subtitles

### 1.0.2

feat

- add `space` config
- remove '[]'

fix

- split error when '\w|'
- split error when '\|'

### 1.0.1

feat

- add arg for reAttribute (row: string[], title: string)

fix

- not support '\|' in table
- not support ||| in table
98 changes: 77 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# components-helper

> Based on the docs to provide code prompt files for vue component library
> Based on the documents to provide code prompt files for vue component library
nodejs ^10
Reference documents format reference test files

[Changelog](./CHANGELOG.md)

## Installation

Expand Down Expand Up @@ -41,19 +43,56 @@ then in package.json

## Options

### entry (required)

- [entry (required)](#entry)
- [outDir (required)](#outdir)
- [name (required)](#name)
- [version (required)](#version)
- [space](#space)
- [separator](#separator)
- [reComponentName](#recomponentname)
- [reDocUrl](#redocurl)
- [reAttribute](#reattribute)
- [reVeturDescription](#reveturdescription)
- [reWebTypesSource](#rewebtypessource)
- [tags](#tags)
- [attributes](#attributes)
- [webTypes](#webtypes)
- [props](#props)
- [propsName](#propsname)
- [propsDescription](#propsdescription)
- [propsType](#propstype)
- [propsOptions](#propsoptions)
- [propsDefault](#propsdefault)
- [events](#events)
- [eventsName](#eventsname)
- [eventsDescription](#eventsdescription)
- [slots](#slots)
- [slotsName](#slotsname)
- [slotsDescription](#slotsdescription)
- [slotsSubtags](#slotssubtags)
- [directives](#directives)
- [directivesName](#directivesname)
- [directivesDescription](#directivesdescription)
- [directivesType](#directivestype)
- [titleRegExp](#titleregexp)
- [tableRegExp](#tableregexp)
- [fileNameRegExp](#filenameregexp)

### entry

- Required: `true`
- Type: `string`

entry path, refer: [fast-glob](https://github.com/mrmlnc/fast-glob#pattern-syntax)

For example:
for example:
- `docs/*.md` -- matches all files in the docs
- `docs/(a|b).md` -- matches files `a.md` and `b.md`
- `docs/!(a|b).md` -- matches files except `a.md` and `b.md`

### outDir (required)
### outDir

- Required: `true`
- Type: `string`

outDir path
Expand All @@ -62,33 +101,56 @@ For example: `lib`

### name

- Required: `true`
- Type: `string`

name of the component library
name of the component library.

### version

- Required: `true`
- Type: `string`

version of the component library
version of the component library.

### space

- Type: `number` | `string`

Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read

### separator

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

the separator for propsOptions, slotsSubtags, type ...

### reComponentName

- Type: `(title: string, fileName: string, path: string) => string`
- Defult: `hyphenate(title || fileName)`

rewriting the name of the component

For example: `(title) => 'prefix-' + title.replace(/\B([A-Z])/g, '-$1').toLowerCase()`
for example: `(title) => 'prefix-' + title.replace(/\B([A-Z])/g, '-$1').toLowerCase()`

### reDocUrl

- Type: `(fileName: string, header?: string, path: string) => string`
- Type: `(fileName: string, header?: string, path: string) => string | undefind`

rewriting the doc url of the component

### reAttribute

- Type: `(value: string, key: string, columns: string[], title: string) => string | undefined`
- Type: `(value: string, key: string, row: string[], title: string) => string | undefined`

##### arg

- value: current value
- key: the key value of the current column
- row: all values of the current row
- title: the title of current tabel

rewriting the attribute of the component

Expand All @@ -99,11 +161,12 @@ rewriting the attribute of the component

rewriting the description of vetur

### space
### reWebTypesSource

- Type: `number` | `string`
- Type: `(title: string, fileName: string, path: string) => { module?: string; symbol: string } | { file: string; offset: number }`
- Default: `{ symbol: title }`

Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read
rewriting the source of web-types. (the name of export from conmonents library)

### tags

Expand All @@ -126,13 +189,6 @@ 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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "components-helper",
"version": "1.0.3",
"version": "1.0.4",
"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 Expand Up @@ -42,6 +42,9 @@
"url": "https://github.com/tolking/components-helper/issues"
},
"homepage": "https://github.com/tolking/components-helper#readme",
"engines": {
"node": ">10.0.0"
},
"dependencies": {
"fast-glob": "^3.2.5"
},
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const config: Config = {
tableRegExp:
'#+\\s+(.*)\\n+(\\|?.+\\|.+)\\n\\|?\\s*:?-+:?\\s*\\|.+((\\n\\|?.+\\|.+)+)',
fileNameRegExp: '\\/((\\w|-)+)\\.\\w+$',
separator: '/',
props: 'props',
propsName: 'Name',
propsType: 'Type',
propsDescription: 'Description',
propsOptions: 'Options',
propsDefault: 'Default',
separator: '/',
events: 'events',
eventsName: 'Name',
eventsDescription: 'Description',
Expand Down
6 changes: 3 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function parseTable(options: Options, str: string): ParseTable {
const { tableRegExp } = options
const tableContent = str.match(new RegExp(tableRegExp))
const title = tableContent ? tableContent[1] : ''
const header = tableContent ? parseColumn(tableContent[2]) : undefined
const header = tableContent ? parseRow(tableContent[2]) : undefined
const columns = tableContent ? tableContent[3] : undefined
let content = [] as ParseTableColumn[]

Expand All @@ -46,7 +46,7 @@ function parseTable(options: Options, str: string): ParseTable {
}
}

function parseColumn(str: string): string[] {
function parseRow(str: string): string[] {
return str
.replace(/^\|/, '')
.replace(/\|$/, '')
Expand All @@ -71,7 +71,7 @@ function parseColumns(

if (item) {
const column = {} as ParseTableColumn
const list = parseColumn(item)
const list = parseRow(item)

list.forEach((value, index) => {
const key = header[index]
Expand Down
29 changes: 26 additions & 3 deletions src/type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
type ReComponentName = (title: string, fileName: string, path: string) => string

type ReDocUrl = (fileName: string, header?: string, path?: string) => string
type ReDocUrl = (
fileName: string,
header?: string,
path?: string,
) => string | undefined

type ReAttribute = (
value: string,
Expand All @@ -13,7 +17,13 @@ type ReVeturDescription = (
description?: string,
defaultValue?: string,
docUrl?: string,
) => string
) => string | undefined

type ReWebTypesSource = (
title: string,
fileName: string,
path: string,
) => Source

interface OptionsConfig {
entry: string
Expand All @@ -25,6 +35,7 @@ interface OptionsConfig {
reDocUrl?: ReDocUrl
reAttribute?: ReAttribute
reVeturDescription?: ReVeturDescription
reWebTypesSource?: ReWebTypesSource
}

export interface Config {
Expand All @@ -34,13 +45,13 @@ export interface Config {
titleRegExp: string
tableRegExp: string
fileNameRegExp: string
separator: string
props: string
propsName: string
propsType: string
propsDescription: string
propsOptions: string
propsDefault: string
separator: string
events: string
eventsName: string
eventsDescription: string
Expand Down Expand Up @@ -126,8 +137,19 @@ export interface WebAttribute {
}
}

export type Source =
| {
module?: string
symbol: string
}
| {
file: string
offset: number
}

export interface WebTag {
name: string
source: Source
description?: string
'doc-url'?: string
attributes?: WebAttribute[]
Expand All @@ -137,6 +159,7 @@ export interface WebTag {

export interface WebDirective {
name: string
source: Source
description?: string
'doc-url'?: string
value?: {
Expand Down
38 changes: 37 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Options } from './type'
import type { Options, Source } from './type'

export function hyphenate(str: string): string {
return str.replace(/\B([A-Z])/g, '-$1').toLowerCase()
Expand Down Expand Up @@ -34,3 +34,39 @@ export function getDocUrl(
const { reDocUrl } = options
return isFunction(reDocUrl) ? reDocUrl(fileName, header, path) : undefined
}

export function getVeturDescription(
options: Options,
description?: string,
defaultVal?: string,
docUrl?: string,
): string | undefined {
const { reVeturDescription } = options

if (isFunction(reVeturDescription)) {
return reVeturDescription(description, defaultVal, docUrl)
} else {
let str = description || ''

if (defaultVal) {
str += `${str ? ', ' : ''}default: ${defaultVal}.`
}
if (docUrl) {
str += `${str ? '\n\n' : ''}[Docs](${docUrl})`
}

return str ? str : undefined
}
}

export function getWebTypesSource(
options: Options,
title = '',
fileName: string,
path: string,
): Source {
const { name, reWebTypesSource } = options
return isFunction(reWebTypesSource)
? reWebTypesSource(title, fileName, path)
: { module: name, symbol: title }
}
Loading

0 comments on commit 61cf40b

Please sign in to comment.