-
Notifications
You must be signed in to change notification settings - Fork 2
/
preview.js
104 lines (98 loc) · 3.11 KB
/
preview.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { setCustomElementsManifest } from "@storybook/web-components";
import customElements from "../custom-elements.json";
import DocumentationTemplate from "./DocumentationTemplate.mdx";
import "@eox/chart";
import "@eox/drawtools";
import "@eox/geosearch";
import "@eox/itemfilter";
import "@eox/jsonform";
import "@eox/layercontrol";
import "@eox/layout";
import "@eox/map";
import "@eox/map/src/plugins/advancedLayersAndSources";
import "@eox/stacinfo";
import "@eox/storytelling";
import "@eox/timecontrol";
/**
* A custom wrapper for the default setCustomElementsManifest function.
* Allows to set `privateFileds` to `false` in order to filter out
* private properties from the auto-generated docs output.
*/
export const setCustomElementsManifestWithOptions = (
customElements,
options,
) => {
let { privateFields = true } = options;
if (!privateFields) {
customElements?.modules?.forEach((module) => {
module?.declarations?.forEach((declaration) => {
Object.keys(declaration).forEach((key) => {
if (Array.isArray(declaration[key])) {
declaration[key] = declaration[key].filter((member) => {
return !member.privacy?.includes("private");
});
}
});
});
});
}
return setCustomElementsManifest(customElements);
};
setCustomElementsManifestWithOptions(customElements, { privateFields: false });
const preview = {
parameters: {
docs: {
toc: true,
page: DocumentationTemplate,
source: {
transform: (code, storyContext) => {
/**
* Check which props belong to which tag
* and create object
*/
const undecorated = storyContext.undecoratedStoryFn(storyContext);
let currentTag = undefined;
const tags = {};
undecorated.strings
.map((string, index) => {
const startOfTag = string.match(/(?<=<eox-).*?((?=>| )|$)/gim);
if (startOfTag) {
currentTag = `eox-${startOfTag}`;
}
const property = string
.match(/(?<=\.).*?(?==)/gim)?.[0]
?.replaceAll(" ", "");
const value = undecorated.values[index];
if (property && value) {
if (!tags[currentTag]) {
tags[currentTag] = {};
}
tags[currentTag][property] = value;
}
})
.filter((l) => l);
/**
* Inject prop for each tag in the rendered code
*/
Object.keys(tags).forEach((tag) => {
code = code.replace(
`<${tag}`,
`<${tag}\n ${Object.keys(tags[tag])
.map(
(key, index) =>
`.${key}='\$\{${
typeof tags[tag][key] === "string"
? tags[tag][key]
: JSON.stringify(tags[tag][key])
}\}'`,
)
.join("\n ")}\n `,
);
});
return code;
},
},
},
},
};
export default preview;