|
| 1 | +/* eslint-disable no-continue */ |
| 2 | +import { Artical, Articals } from './parser'; |
| 3 | +import { formatType, removeVersion, toKebabCase } from './utils'; |
| 4 | +import { VueTag } from './type'; |
| 5 | + |
| 6 | +function getComponentName(name: string, tagPrefix: string) { |
| 7 | + if (name) { |
| 8 | + return tagPrefix + toKebabCase(name.split(' ')[0]); |
| 9 | + } |
| 10 | + return ''; |
| 11 | +} |
| 12 | + |
| 13 | +function parserProps(tag: VueTag, line: any) { |
| 14 | + const [name, desc, type, defaultVal] = line; |
| 15 | + if ( |
| 16 | + type && |
| 17 | + (type.includes('v-slot') || |
| 18 | + type.includes('slot') || |
| 19 | + type.includes('slots') || |
| 20 | + type.includes('slot-scoped')) |
| 21 | + ) { |
| 22 | + tag.slots!.push({ |
| 23 | + name: removeVersion(name), |
| 24 | + description: desc, |
| 25 | + }); |
| 26 | + } |
| 27 | + tag.attributes!.push({ |
| 28 | + name: removeVersion(name), |
| 29 | + default: defaultVal, |
| 30 | + description: desc, |
| 31 | + value: { |
| 32 | + type: formatType(type || ''), |
| 33 | + kind: 'expression', |
| 34 | + }, |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +export function formatter(articals: Articals, componentName: string, tagPrefix: string = '') { |
| 39 | + if (!articals.length) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + const tags: VueTag[] = []; |
| 44 | + const tag: VueTag = { |
| 45 | + name: getComponentName(componentName, tagPrefix), |
| 46 | + slots: [], |
| 47 | + events: [], |
| 48 | + attributes: [], |
| 49 | + }; |
| 50 | + tags.push(tag); |
| 51 | + |
| 52 | + const tables = articals.filter(artical => artical.type === 'table'); |
| 53 | + |
| 54 | + tables.forEach(item => { |
| 55 | + const { table } = item; |
| 56 | + const prevIndex = articals.indexOf(item) - 1; |
| 57 | + const prevArtical = articals[prevIndex]; |
| 58 | + |
| 59 | + if (!prevArtical || !prevArtical.content || !table || !table.body) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const tableTitle = prevArtical.content; |
| 64 | + |
| 65 | + if (tableTitle.includes('API')) { |
| 66 | + table.body.forEach(line => { |
| 67 | + parserProps(tag, line); |
| 68 | + }); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + if (tableTitle.includes('events') && !tableTitle.includes(componentName)) { |
| 73 | + table.body.forEach(line => { |
| 74 | + const [name, desc] = line; |
| 75 | + tag.events!.push({ |
| 76 | + name: removeVersion(name), |
| 77 | + description: desc, |
| 78 | + }); |
| 79 | + }); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // 额外的子组件 |
| 84 | + if (tableTitle.includes(componentName) && !tableTitle.includes('events')) { |
| 85 | + const childTag: VueTag = { |
| 86 | + name: getComponentName(tableTitle.replace('.', ''), tagPrefix), |
| 87 | + slots: [], |
| 88 | + events: [], |
| 89 | + attributes: [], |
| 90 | + }; |
| 91 | + table.body.forEach(line => { |
| 92 | + parserProps(childTag, line); |
| 93 | + }); |
| 94 | + tags.push(childTag); |
| 95 | + return; |
| 96 | + } |
| 97 | + // 额外的子组件事件 |
| 98 | + if (tableTitle.includes(componentName) && tableTitle.includes('events')) { |
| 99 | + const childTagName = getComponentName( |
| 100 | + tableTitle.replace('.', '').replace('events', ''), |
| 101 | + tagPrefix, |
| 102 | + ); |
| 103 | + const childTag: VueTag | undefined = tags.find(item => item.name === childTagName.trim()); |
| 104 | + if (!childTag) { |
| 105 | + return; |
| 106 | + } |
| 107 | + table.body.forEach(line => { |
| 108 | + const [name, desc] = line; |
| 109 | + childTag.events!.push({ |
| 110 | + name: removeVersion(name), |
| 111 | + description: desc, |
| 112 | + }); |
| 113 | + }); |
| 114 | + return; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + return tags; |
| 119 | +} |
0 commit comments