@rspress-theme-anatole/plugin-container-syntax 0.1.31 → 0.2.0

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.
Files changed (2) hide show
  1. package/dist/index.js +49 -50
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -23,6 +23,8 @@ const parseTitle = (rawTitle = '', isMDX = false) => {
23
23
  };
24
24
 
25
25
  const createContainer = (type, title, children) => {
26
+ // console.log('createContainer', JSON.stringify(children, null, 2));
27
+
26
28
  const isDetails = 'details' === type;
27
29
  const isTabs = 'tabs' === type;
28
30
  const rootHName = isDetails ? 'details' : 'div';
@@ -32,8 +34,8 @@ const createContainer = (type, title, children) => {
32
34
  if (isTabs) {
33
35
  // Identify tab sections by finding paragraphs starting with ==
34
36
  const tabSections = [];
35
- let currentTabContent = null;
36
37
  let currentTabTitle = '';
38
+ let currentTabContent = null;
37
39
 
38
40
  // Process children to find tab sections
39
41
  for (let i = 0; i < children.length; i++) {
@@ -44,7 +46,6 @@ const createContainer = (type, title, children) => {
44
46
  child.children[0] &&
45
47
  child.children[0].type === 'text' &&
46
48
  child.children[0].value.trim().startsWith('==')) {
47
-
48
49
  // If we already have a tab section, push it
49
50
  if (currentTabContent) {
50
51
  tabSections.push({
@@ -53,9 +54,21 @@ const createContainer = (type, title, children) => {
53
54
  });
54
55
  }
55
56
 
56
- // Start a new tab section
57
- currentTabTitle = child.children[0].value.trim().substring(2).trim();
58
- currentTabContent = [];
57
+ let text = child.children[0].value.trim();
58
+
59
+ if (text.includes('\r\n')) {
60
+ let match = text.split('\r\n', 2);
61
+ currentTabTitle = match[0].substring(2).trim();
62
+ currentTabContent = [{
63
+ type: 'text',
64
+ value: text.substring(match[0].length).trim()
65
+ }];
66
+ }
67
+ else {
68
+ currentTabTitle = text.substring(2).trim();
69
+ currentTabContent = [];
70
+ }
71
+
59
72
  continue;
60
73
  }
61
74
 
@@ -178,36 +191,14 @@ const createContainer = (type, title, children) => {
178
191
  };
179
192
  };
180
193
 
181
- export function initializeTabs() {
182
- document.addEventListener('DOMContentLoaded', () => {
183
- const tabContainers = document.querySelectorAll('.rspress-directive.tabs');
184
-
185
- tabContainers.forEach(container => {
186
- const navItems = container.querySelectorAll('.tabs-nav-item');
187
- const panels = container.querySelectorAll('.tabs-panel');
188
-
189
- navItems.forEach((navItem, index) => {
190
- navItem.addEventListener('click', () => {
191
- // Remove active class from all nav items and panels
192
- navItems.forEach(item => item.classList.remove('active'));
193
- panels.forEach(panel => panel.classList.remove('active'));
194
-
195
- // Add active class to clicked nav item and corresponding panel
196
- navItem.classList.add('active');
197
- panels[index].classList.add('active');
198
- });
199
- });
200
- });
201
- });
202
- }
203
-
204
-
205
194
  function transformer(tree) {
206
195
  let i = 0;
207
196
  try {
208
197
  while (i < tree.children.length) {
209
198
  const node = tree.children[i];
210
- if ('children' in node) transformer(node);
199
+ if ('children' in node) {
200
+ transformer(node);
201
+ }
211
202
  if ('containerDirective' === node.type) {
212
203
  const type = node.name;
213
204
  if (DIRECTIVE_TYPES.includes(type)) tree.children.splice(i, 1, createContainer(type, node.attributes?.title ?? type.toUpperCase(), node.children));
@@ -229,6 +220,7 @@ function transformer(tree) {
229
220
  i++;
230
221
  continue;
231
222
  }
223
+
232
224
  const firstTextNode = node.children[0];
233
225
  const text = firstTextNode.value;
234
226
  const metaText = text.split('\n')[0];
@@ -250,7 +242,7 @@ function transformer(tree) {
250
242
  continue;
251
243
  }
252
244
  const wrappedChildren = [];
253
- if (content?.endsWith(':::')) {
245
+ if (node.type === 'text' && node.value.endsWith(':::')) {
254
246
  wrappedChildren.push({
255
247
  type: 'paragraph',
256
248
  children: [
@@ -273,30 +265,40 @@ function transformer(tree) {
273
265
  value: content
274
266
  });
275
267
  paragraphChild.children.push(...node.children.slice(1, -1));
268
+
276
269
  if (0 === paragraphChild.children.length) wrappedChildren.pop();
270
+
277
271
  const lastChildInNode = node.children[node.children.length - 1];
272
+
278
273
  if ('text' === lastChildInNode.type && REGEX_END.test(lastChildInNode.value)) {
279
274
  const lastChildInNodeText = lastChildInNode.value;
280
275
  const matchedEndContent = lastChildInNodeText.slice(0, -3).trim();
281
- if (wrappedChildren.length) wrappedChildren[0].children.push({
282
- type: 'text',
283
- value: matchedEndContent
284
- });
285
- else if (matchedEndContent) wrappedChildren.push({
286
- type: 'paragraph',
287
- children: [
288
- {
289
- type: 'text',
290
- value: matchedEndContent
291
- }
292
- ]
293
- });
276
+ if (wrappedChildren.length) {
277
+ wrappedChildren[0].children.push({
278
+ type: 'text',
279
+ value: matchedEndContent
280
+ });
281
+ }
282
+ else if (matchedEndContent) {
283
+ wrappedChildren.push({
284
+ type: 'paragraph',
285
+ children: [
286
+ {
287
+ type: 'text',
288
+ value: matchedEndContent
289
+ }
290
+ ]
291
+ });
292
+ }
293
+
294
294
  const newChild = createContainer(type, title, wrappedChildren);
295
295
  tree.children.splice(i, 1, newChild);
296
296
  i++;
297
297
  continue;
298
298
  }
299
- if (lastChildInNode !== firstTextNode && wrappedChildren.length) wrappedChildren[0].children.push(lastChildInNode);
299
+ if (lastChildInNode !== firstTextNode && wrappedChildren.length) {
300
+ wrappedChildren[0].children.push(lastChildInNode);
301
+ }
300
302
  let j = i + 1;
301
303
  while (j < tree.children.length) {
302
304
  const currentParagraph = tree.children[j];
@@ -306,6 +308,7 @@ function transformer(tree) {
306
308
  continue;
307
309
  }
308
310
  const lastChild = currentParagraph.children[currentParagraph.children.length - 1];
311
+
309
312
  if (lastChild === firstTextNode || 'text' === lastChild.type && REGEX_END.test(lastChild.value)) {
310
313
  const lastChildText = lastChild.value;
311
314
  const matchedEndContent = lastChildText.slice(0, -3).trim();
@@ -319,6 +322,7 @@ function transformer(tree) {
319
322
  }
320
323
  ]
321
324
  });
325
+
322
326
  const newChild = createContainer(type, title, wrappedChildren);
323
327
  tree.children.splice(i, j - i + 1, newChild);
324
328
  break;
@@ -341,11 +345,6 @@ function transformer(tree) {
341
345
  const remarkPluginContainer = () => transformer;
342
346
  const src_dirname = (0, __WEBPACK_EXTERNAL_MODULE__rspress_shared_baa012d0__.normalizePosixPath)(__WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].dirname((0, __WEBPACK_EXTERNAL_MODULE_node_url_e96de089__.fileURLToPath)(import.meta.url)));
343
347
  function pluginContainerSyntax() {
344
- // Initialize tabs when the plugin loads
345
- if (typeof window !== 'undefined') {
346
- initializeTabs();
347
- }
348
-
349
348
  return {
350
349
  name: '@rspress-theme-anatole/plugin-container-syntax',
351
350
  markdown: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspress-theme-anatole/plugin-container-syntax",
3
- "version": "0.1.31",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "sideEffects": [
6
6
  "*.css",
@@ -17,7 +17,7 @@
17
17
  "container.css"
18
18
  ],
19
19
  "dependencies": {
20
- "@rspress-theme-anatole/shared": "0.1.30"
20
+ "@rspress-theme-anatole/shared": "0.2.0"
21
21
  },
22
22
  "devDependencies": {
23
23
  "@microsoft/api-extractor": "^7.49.2",