-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from BlackFenix2/svelte-docgen
Svelte docgen
- Loading branch information
Showing
8 changed files
with
287 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,5 @@ | |
}); | ||
</script> | ||
|
||
<svelte:options accessors={true} /> | ||
<div id={hash} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
import { extractArgTypes } from './extractArgTypes'; | ||
import { extractComponentDescription } from '../../lib/docgen'; | ||
import { prepareForInline } from './prepareForInline'; | ||
|
||
export const parameters = { | ||
docs: { | ||
inlineStories: true, | ||
prepareForInline, | ||
extractArgTypes, | ||
extractComponentDescription, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { ArgTypes } from '@storybook/api'; | ||
|
||
import { ArgTypesExtractor, hasDocgen, extractComponentProps } from '../../lib/docgen'; | ||
import { convert } from '../../lib/convert'; | ||
import { trimQuotes } from '../../lib/convert/utils'; | ||
|
||
const SECTIONS = ['props', 'events', 'slots']; | ||
|
||
const trim = (val: any) => (val && typeof val === 'string' ? trimQuotes(val) : val); | ||
|
||
type ComponentWithDocgen = { | ||
__docgen: { | ||
components: []; | ||
computed: []; | ||
data: [ | ||
{ | ||
defaultValue: any; | ||
description: string; | ||
keywords: []; | ||
kind: string; | ||
name: string; | ||
readonly: boolean; | ||
static: boolean; | ||
type: { kind: string; text: string; type: string }; | ||
visibility: string; | ||
} | ||
]; | ||
description: null; | ||
events: []; | ||
keywords: []; | ||
methods: []; | ||
name: null; | ||
refs: []; | ||
slots: []; | ||
version: 3; | ||
}; | ||
}; | ||
|
||
export const extractArgTypes: ArgTypesExtractor = (component) => { | ||
const item = new component({ props: {} }); | ||
console.log(item.__docgen); | ||
const results: ArgTypes = { | ||
rounded: { | ||
control: { type: 'boolean' }, | ||
name: 'rounded', | ||
description: 'round the button', | ||
defaultValue: false, | ||
|
||
table: { | ||
defaultValue: { | ||
summary: false, | ||
}, | ||
}, | ||
}, | ||
text: { | ||
control: { type: 'text' }, | ||
name: 'text', | ||
description: 'descriptive text', | ||
defaultValue: 'You Clicked', | ||
table: { | ||
defaultValue: { | ||
summary: 'your text here', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
return results; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import path from 'path'; | ||
|
||
import { Configuration } from 'webpack'; | ||
|
||
export function webpackFinal(webpackConfig: Configuration, options: any = {}) { | ||
webpackConfig.module.rules.push({ | ||
test: /\.svelte$/, | ||
loader: path.resolve(`${__dirname}/svelte-docgen-loader`), | ||
enforce: 'post', | ||
}); | ||
|
||
return webpackConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable no-param-reassign */ | ||
// @ts-ignore | ||
import svelteDoc from 'sveltedoc-parser'; | ||
|
||
import * as path from 'path'; | ||
|
||
import * as fs from 'fs'; | ||
import { string } from 'prop-types'; | ||
|
||
const SVELTE_DOCGEN = {}; | ||
/** | ||
* webpack loader for sveltedoc-parser | ||
* @param source raw svelte component | ||
*/ | ||
export default async function svelteDocgen(source: string) { | ||
const file = path.basename(this._module.resource); | ||
const options = { | ||
fileContent: source, | ||
version: 3, | ||
}; | ||
|
||
console.log('File: ', file); | ||
let docgen = ''; | ||
|
||
try { | ||
const componentDoc = await svelteDoc.parse(options); | ||
docgen = ` | ||
export const __docgen = ${JSON.stringify(componentDoc)}; | ||
`; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
|
||
const output = source.replace('</script>', `${docgen}</script>`); | ||
|
||
return output; | ||
} | ||
|
||
function insert(str: string, index: number, value: string) { | ||
return str.substr(0, index) + value + str.substr(index); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.