@rspress-theme-anatole/theme-default 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,235 @@
1
+ import React, { useState, Children, isValidElement } from 'react';
2
+ import ReactDOMServer from 'react-dom/server';
3
+
4
+ interface TabItem {
5
+ label: string;
6
+ content: string;
7
+ }
8
+
9
+ interface TabsProps {
10
+ children: React.ReactNode;
11
+ }
12
+
13
+ export const Tabs: React.FC<TabsProps> = ({ children }) => {
14
+ // console.log(children);
15
+ const outputItems: TabItem[] = [];
16
+
17
+ const convertToHTML = (node: any): string => {
18
+ // console.log(node);
19
+ if (node === null || node === undefined) return '';
20
+
21
+ if (typeof node === 'string' || typeof node === 'number') {
22
+ return String(node);
23
+ }
24
+
25
+ if (Array.isArray(node)) {
26
+ return node.map(item => {
27
+ return convertToHTML(item);
28
+ }
29
+ ).join('');
30
+ }
31
+
32
+ if (isValidElement(node)) {
33
+ const element = node as React.ReactElement;
34
+ const props = element.props as Record<string, any>;
35
+
36
+ if (element.type === React.Fragment) {
37
+ return convertToHTML(props.children);
38
+ }
39
+
40
+ if (typeof element.type === 'string') {
41
+ // Build attributes string
42
+ const attributes = Object.entries(props)
43
+ .filter(([key]) => key !== 'children' && key !== 'className')
44
+ .map(([key, value]) => `${key}="${value}"`)
45
+ .concat(props.className ? [`class="${props.className}"`] : [])
46
+ .join(' ');
47
+
48
+ const attributeString = attributes ? ` ${attributes}` : '';
49
+
50
+ // Convert children to HTML
51
+ const childrenContent = props.children ? convertToHTML(props.children) : '';
52
+
53
+ // Handle void elements
54
+ const voidElements = ['img', 'br', 'hr', 'input', 'meta', 'link'];
55
+ if (voidElements.includes(element.type)) {
56
+ return `<${element.type}${attributeString}>`;
57
+ }
58
+
59
+ // Handle regular elements
60
+ return `<${element.type}${attributeString}>${childrenContent}</${element.type}>`;
61
+ }
62
+
63
+ try {
64
+ return ReactDOMServer.renderToStaticMarkup(element);
65
+ } catch (error) {
66
+ // return convertToHTML(props.children);
67
+ if (element) {
68
+ const modifiedElement = {
69
+ ...element,
70
+ props: {
71
+ ...(typeof element.props === 'object' && element.props !== null ? element.props : {}),
72
+ children: DealWithUrl(props.children)
73
+ }
74
+ };
75
+
76
+ // console.log(modifiedElement);
77
+ if (modifiedElement) {
78
+ try {
79
+ return ReactDOMServer.renderToStaticMarkup(modifiedElement);
80
+ }
81
+ catch {
82
+ return convertToHTML(props.children);
83
+ }
84
+ }
85
+ }
86
+ }
87
+ }
88
+
89
+ // Handle objects with props (like context providers)
90
+ if (node && typeof node === 'object' && 'props' in node) {
91
+ return convertToHTML((node as any).props.children);
92
+ }
93
+
94
+ return '';
95
+ };
96
+
97
+ function DealWithUrl(para: any): any {
98
+ if (Array.isArray(para)) {
99
+ return para.map((child: any) => {
100
+ if (child && child.type) {
101
+ // console.log(child.props);
102
+ if ("href" in child.props) {
103
+ return child.props.children;
104
+ }
105
+ else if (child.props && child.props.children) {
106
+ return {
107
+ ...child,
108
+ props: {
109
+ ...child.props,
110
+ children: DealWithUrl(child.props.children)
111
+ }
112
+ };
113
+ }
114
+ }
115
+ return child;
116
+ });
117
+ } else {
118
+ return para;
119
+ }
120
+ }
121
+
122
+ // function convertUrlsToLinks(content: string) {
123
+ // const urlPattern = /(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/g;
124
+ // return content.replace(urlPattern, function (url) {
125
+ // return `<a href="${encodeURI(url)}" target="_blank">${url}</a>`;
126
+ // });
127
+ // }
128
+
129
+ const parseTabs = (inputItems: React.ReactNode): TabItem[] => {
130
+ Children.forEach(inputItems, (inputItem) => {
131
+ if (isValidElement<{ children: React.ReactNode }>(inputItem)) {
132
+ if (typeof inputItem.props.children === 'string') {
133
+ const text: string = inputItem.props.children;
134
+ if (text.startsWith('== ')) {
135
+ if (text.includes('\r\n')) {
136
+ let match = text.split('\r\n', 2);
137
+ var tabItem: TabItem = {
138
+ label: match[0].split('==').filter(Boolean)[0],
139
+ content: text.substring(match[0].length).trim()
140
+ };
141
+ }
142
+ else {
143
+ var tabItem: TabItem = {
144
+ label: text.split('==').filter(Boolean)[0],
145
+ content: ''
146
+ };
147
+ }
148
+
149
+ outputItems.push(tabItem);
150
+ }
151
+ else {
152
+ const lastTab = outputItems[outputItems.length - 1];
153
+ if (lastTab) {
154
+ const htmlContent = convertToHTML(inputItem);
155
+ lastTab.content += htmlContent;
156
+ }
157
+ }
158
+ }
159
+ else {
160
+ const lastTab = outputItems[outputItems.length - 1];
161
+ if (lastTab) {
162
+ const htmlContent = convertToHTML(inputItem);
163
+ lastTab.content += htmlContent;
164
+ }
165
+ }
166
+ }
167
+ });
168
+ return outputItems;
169
+ };
170
+
171
+ const tabs = parseTabs(children);
172
+ const [activeTab, setActiveTab] = useState(tabs[0]?.label);
173
+
174
+ if (!tabs.length) return null;
175
+
176
+ return (
177
+ <div className="tabs-container">
178
+ <div className="tabs-header">
179
+ {tabs.map((tab) => (
180
+ <button
181
+ key={tab.label}
182
+ className={`tab-button ${activeTab === tab.label ? 'active' : ''}`}
183
+ onClick={() => setActiveTab(tab.label)}
184
+ >
185
+ {tab.label}
186
+ </button>
187
+ ))}
188
+ </div>
189
+ <div className="tabs-content"
190
+ dangerouslySetInnerHTML={{ __html: tabs.find((tab) => tab.label === activeTab)?.content ?? '' }}>
191
+ </div>
192
+
193
+ <style>{`
194
+ .tabs-container {
195
+ margin: 1rem 0;
196
+ border: 1px solid #e5e7eb;
197
+ border-radius: 0.375rem;
198
+ }
199
+ .tabs-header {
200
+ display: flex;
201
+ border-bottom: 1px solid #e5e7eb;
202
+ background-color: #f9fafb;
203
+ border-top-left-radius: 0.375rem;
204
+ border-top-right-radius: 0.375rem;
205
+ }
206
+ .tab-button {
207
+ padding: 0.75rem 1.25rem;
208
+ border: none;
209
+ background: none;
210
+ cursor: pointer;
211
+ font-size: 0.875rem;
212
+ color: #4b5563;
213
+ border-right: 1px solid #e5e7eb;
214
+ }
215
+ .tab-button:hover {
216
+ background-color: #f3f4f6;
217
+ }
218
+ .tab-button.active {
219
+ color: #1e40af;
220
+ background-color: #fff;
221
+ border-bottom: 2px solid #1e40af;
222
+ margin-bottom: -1px;
223
+ }
224
+ .tabs-content {
225
+ padding: 1rem;
226
+ background-color: #fff;
227
+ border-bottom-left-radius: 0.375rem;
228
+ border-bottom-right-radius: 0.375rem;
229
+ }
230
+ `}</style>
231
+ </div>
232
+ );
233
+ };
234
+
235
+ export default Tabs;
package/config.ts CHANGED
@@ -1,7 +1,21 @@
1
+ import * as path from 'node:path';
1
2
  import type { UserConfig } from '@rspress-theme-anatole/shared';
3
+ import directives from '@rspress-theme-anatole/rspress-plugin-directives';
2
4
 
3
5
  export function defineConfig(config: UserConfig): UserConfig {
4
- return config;
6
+ return {
7
+ ...config,
8
+ plugins: [
9
+ directives({
10
+ directive: 'tabs',
11
+ transformer: {
12
+ type: 'globalComponent',
13
+ getComponentName: (meta) => 'Tabs',
14
+ componentPath: path.join(__dirname, './components/tabs.tsx'),
15
+ },
16
+ }),
17
+ ],
18
+ }
5
19
  }
6
20
 
7
21
  export type { UserConfig };
package/dist/bundle.js CHANGED
@@ -2938,7 +2938,6 @@ function HomepageTopSearch({ frontmatter, routePath }) {
2938
2938
  ],
2939
2939
  style: {
2940
2940
  height: "400px",
2941
- backgroundColor: "green",
2942
2941
  backgroundImage: `url(${(0, __WEBPACK_EXTERNAL_MODULE__rspress_runtime_0abd3046__.normalizeImagePath)(imageSrc.light)})`,
2943
2942
  backgroundSize: "cover",
2944
2943
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rspress-theme-anatole/theme-default",
3
3
  "author": "Anatole Tong",
4
- "version": "0.1.19",
4
+ "version": "0.1.21",
5
5
  "license": "MIT",
6
6
  "sideEffects": [
7
7
  "*.css",