-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcode_example.vue
More file actions
194 lines (183 loc) · 5 KB
/
code_example.vue
File metadata and controls
194 lines (183 loc) · 5 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<template>
<dt-tab-group
class="code-example-tab-group"
@change="selectedPanelId = $event.selected"
>
<template #tabs>
<dt-stack
direction="row"
justify="between"
align="start"
class="d-w100p"
>
<div>
<dt-tab
:id="vueTabId"
label="Vue code"
:panel-id="vuePanelId"
selected
>
Vue
</dt-tab>
<dt-tab
v-if="!!htmlCode"
:id="htmlTabId"
label="HTML code"
:panel-id="htmlPanelId"
>
HTML
</dt-tab>
</div>
<!-- aria label blank so no tooltip displays since it would be redundant to the "Copy" text -->
<copy-button
:text="selectedPanelId === htmlPanelId ? trimmedHtmlCode : trimmedVueCode"
aria-label=""
>
Copy
</copy-button>
</dt-stack>
</template>
<dt-tab-panel
:id="vuePanelId"
:tab-id="vueTabId"
>
<div
v-dt-scrollbar
class="language-html d-hmx-500"
data-ext="html"
>
<pre
class="language-html"
v-html="highlightedVue"
/>
</div>
</dt-tab-panel>
<dt-tab-panel
v-if="!!htmlCode"
:id="htmlPanelId"
:tab-id="htmlTabId"
>
<dt-banner
v-if="showHtmlWarning"
class="d-ps-static"
kind="warning"
:show-close="false"
>
Raw HTML renders visuals only. You may need to add JS to replicate its functionality.
</dt-banner>
<div
v-dt-scrollbar
class="language-html d-hmx-500"
data-ext="html"
>
<pre
class="language-html"
v-html="highlightedHtml"
/>
</div>
</dt-tab-panel>
</dt-tab-group>
</template>
<!--
TODO: Get the HTML and Vue code working
This was copied from `CodeExampleTabs.vue` component,
get this working and replace on documentation site with combinator.
-->
<script setup>
import { computed, onMounted, ref } from 'vue';
import Prism from 'prismjs';
import prettier from 'prettier/standalone';
import htmlParser from 'prettier/plugins/html.mjs';
import CopyButton from './CopyButton.vue';
import { getUniqueString } from '@workspaceRoot/common/utils/client.mjs';
const props = defineProps({
/**
* The HTML code to be displayed in the HTML tab if the component reference is not provided or
* a function that retrieves the reference to the example component to get the HTML code.
*/
htmlCode: {
type: [String, Function],
default: null,
},
/**
* The Vue code to be displayed in the Vue tab.
*/
vueCode: {
type: String,
required: true,
},
/**
* Indicates whether to show a warning for HTML code.
*/
showHtmlWarning: {
type: Boolean,
default: true,
},
});
const formattedHTML = ref(null);
const trimmedHtmlCode = computed(() => {
if (formattedHTML.value) {
return formattedHTML.value;
}
return typeof props.htmlCode === 'string' ? props.htmlCode.replace(/^\n/gm, '') : '';
});
const highlightedHtml = computed(() => {
if (formattedHTML.value) {
return Prism.highlight(
formattedHTML.value,
Prism.languages.html,
'html',
);
}
return typeof props.htmlCode === 'string' ? Prism.highlight(props.htmlCode.trim(), Prism.languages.html, 'html') : '';
});
const trimmedVueCode = props.vueCode.replace(/^\n/gm, '');
const highlightedVue = Prism.highlight(props.vueCode.trim(), Prism.languages.html, 'html');
const vueTabId = getUniqueString();
const vuePanelId = getUniqueString();
const htmlTabId = getUniqueString();
const htmlPanelId = getUniqueString();
const selectedPanelId = ref(vuePanelId);
onMounted(async () => {
if (typeof props.htmlCode === 'function') {
const componentRef = props.htmlCode();
const el = componentRef.$el ?? componentRef;
const formatted = await formatHTML(el.outerHTML);
formattedHTML.value = formatted;
}
});
/**
* Transforms a single-line HTML string to an indented multiline HTML string.
* Removes comments, id and data-qa attributes, and simplifies svg tags.
* Also, adds a new line before each svg, img, and span tag because prettier
* doesn't do it.
* @param elementHTML - The HTML code to be formatted.
* @returns The formatted HTML code.
*/
const formatHTML = async (elementHTML) => {
const normalizedHTML = elementHTML
.replace(/<!--.*?-->/g, '')
.replace(/id=".*?"/g, '')
.replace(/data-qa=".*?"/g, '')
.replace(/<svg.*?>.*?<\/svg>/g, '<svg>...</svg>')
.replace(/<svg/g, '\n<svg')
.replace(/<img/g, '\n<img')
.replace(/<span/g, '\n<span');
const prettyHTML = await prettier.format(normalizedHTML,
{
parser: 'html',
plugins: [htmlParser],
htmlWhitespaceSensitivity: 'ignore',
});
return prettyHTML;
};
</script>
<style scoped lang="less">
.code-example-tab-group {
margin-block-start: var(--dt-spacing-200);
.language-html {
margin-block-start: 0;
position: relative;
}
}
</style>