Yuki Takei hai 1 ano
pai
achega
881b95f237

+ 10 - 8
bin/data-migrations/src/migrations/v60x/remark-growi-directive/README.ja.md

@@ -2,17 +2,19 @@
 
 以下の要領で replace する
 
-## 1. HTMLタグ内で `$lsx()` を利用している箇所
+なお、`$foo()` は一例であり、`$bar()`, `$baz()`, `$foo-2()` など、さまざまな directive に対応する必要がある
+
+## 1. HTMLタグ内で `$foo()` を利用している箇所
 - 置換対象文章の詳細
-  - `$lsx()`がHTMLタグ内かつ、当該`$lsx()`記述の1行前が空行ではない場合に1行前に空行を挿入
-  - `$lsx()`がHTMLタグ内かつ、`$lsx()`記述行の行頭にインデントがついている場合に当該行のインデントを削除
-  - `$lsx()`がHTMLタグ内かつ、当該`$lsx()`記述の1行後のHTMLタグ記述行にインデントがついている場合にその行頭のインデントを削除
+  - `$foo()`がHTMLタグ内かつ、当該`$foo()`記述の1行前が空行ではない場合に1行前に空行を挿入する
+  - `$foo()`がHTMLタグ内かつ、`$foo()`記述行の行頭にインデントがついている場合に当該行のインデントを削除する
+  - `$foo()`がHTMLタグ内かつ、当該`$foo()`記述の1行後のHTMLタグ記述行にインデントがついている場合にその行頭のインデントを削除する
 
-## 2. `$lsx()` を利用している箇所
+## 2. `$foo()` を利用している箇所
 - 置換対象文章の詳細
-  - `$lsx()`の引数内で括弧 `()` を使用している場合、括弧を削除
-    - before: `$lsx()`(depth=2, filter=(業務課))
-    - after: `$lsx()`(depth=2, filter=業務課)
+  - `$foo()`の引数内で `filter=` あるいは `except=` に対する値に括弧 `()` を使用している場合、括弧を削除する
+    - before: `$foo()`(depth=2, filter=(AAA), except=(BBB))
+    - after: `$foo()`(depth=2, filter=AAA, except=BBB)
 
 ## テストについて
 

+ 39 - 0
bin/data-migrations/src/migrations/v60x/remark-growi-directive/example-expected.md

@@ -0,0 +1,39 @@
+# Header
+
+<div class="container-fluid">
+    <div class="row sampleRow">
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">FOO</div>
+
+$foo(depth=2, filter=(FOO))
+        </div>
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">BAR</div>
+
+$bar(depth=2, filter=BAR)
+        </div>
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">BAZ</div>
+
+$baz(depth=2, filter=baz)
+</div>
+    </div>
+    <hr>
+    <div class="row sampleRow">
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">FOO</div>
+
+$foo-2(depth=2, depth=1, except=(word1|word2|word3))
+</div>
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">BAR</div>
+
+$bar-2(depth=2, depth=1, except=(word1|word2|word3))
+</div>
+        <div class="col-md-4">
+                <div class="lsxContainerTitle">BAZ</div>
+
+$baz-2(depth=2, depth=1, except=(word1|word2|word3))
+</div>
+    </div>
+</div>

+ 33 - 0
bin/data-migrations/src/migrations/v60x/remark-growi-directive/example.md

@@ -0,0 +1,33 @@
+# Header
+
+<div class="container-fluid">
+    <div class="row sampleRow">
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">FOO</div>
+            $lsx(depth=2, filter=(FOO))
+        </div>
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">BAR</div>
+            $lsx(depth=2, filter=(BAR))
+        </div>
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">BAZ</div>
+            $lsx(depth=2, filter=(BAZ))
+        </div>
+    </div>
+    <hr>
+    <div class="row sampleRow">
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">FOO</div>
+            $lsx(depth=2, filter=(FOO))
+        </div>
+        <div class="col-md-4">
+            <div class="lsxContainerTitle">BAR</div>
+            $lsx(depth=2, filter=(BAR))
+        </div>
+        <div class="col-md-4">
+                <div class="lsxContainerTitle">BAZ</div>
+            $lsx(depth=2, filter=(BAZ))
+        </div>
+    </div>
+</div>

+ 9 - 14
bin/data-migrations/src/migrations/v60x/remark-growi-directive/remark-growi-directive.js

@@ -4,33 +4,27 @@
 
 module.exports = [
   /**
-   * Adjust line breaks and indentation for $lsx() within HTML tags
+   * Adjust line breaks and indentation for any directives within HTML tags
    * @type {MigrationModule}
    */
   (body) => {
-    // Split into lines for better processing
     const lines = body.split('\n');
+    const directivePattern = /\$[\w-]+\([^)]*\)/;
 
     for (let i = 0; i < lines.length; i++) {
-      // Find lines containing $lsx()
-      if (lines[i].includes('$lsx(')) {
+      if (directivePattern.test(lines[i])) {
         const currentLine = lines[i];
         const prevLine = i > 0 ? lines[i - 1] : '';
         const nextLine = i < lines.length - 1 ? lines[i + 1] : '';
 
-        // Remove indentation from current line
         lines[i] = currentLine.trimStart();
 
-        // If previous line contains an HTML tag and is not an empty line
         if (prevLine.includes('>') && prevLine.trim() !== '') {
-          // Insert empty line
           lines.splice(i, 0, '');
-          i++; // Adjust index after insertion
+          i++;
         }
 
-        // If next line contains an HTML tag
         if (nextLine.includes('</')) {
-          // Handle next line (remove indentation)
           lines[i + 1] = nextLine.trimStart();
         }
       }
@@ -40,13 +34,14 @@ module.exports = [
   },
 
   /**
-   * Remove unnecessary parentheses in $lsx() filter arguments
+   * Remove unnecessary parentheses in directive arguments
    * @type {MigrationModule}
    */
   (body) => {
-    return body.replace(/\$lsx\([^)]*\)/g, (match) => {
-      // Find and remove parentheses in filter=(...) pattern
-      return match.replace(/filter=\(([^)]+)\)/g, 'filter=$1');
+    return body.replace(/\$[\w-]+\([^)]*\)/g, (match) => {
+      return match
+        .replace(/filter=\(([^)]+)\)/g, 'filter=$1')
+        .replace(/except=\(([^)]+)\)/g, 'except=$1');
     });
   },
 ];

+ 45 - 0
bin/data-migrations/src/migrations/v60x/remark-growi-directive/remark-growi-directive.spec.js

@@ -0,0 +1,45 @@
+import fs from 'node:fs';
+import path from 'node:path';
+
+import { describe, test, expect } from 'vitest';
+
+import migrations from './remark-growi-directive';
+
+describe('remark-growi-directive migrations', () => {
+  test('should transform example.md to match example-expected.md', () => {
+    const input = fs.readFileSync(path.join(__dirname, 'example.md'), 'utf8');
+    const expected = fs.readFileSync(path.join(__dirname, 'example-expected.md'), 'utf8');
+
+    const result = migrations.reduce((text, migration) => migration(text), input);
+    expect(result).toBe(expected);
+  });
+
+  test('should not modify example-expected.md', () => {
+    const input = fs.readFileSync(path.join(__dirname, 'example-expected.md'), 'utf8');
+
+    const result = migrations.reduce((text, migration) => migration(text), input);
+    expect(result).toBe(input);
+  });
+
+  test('should handle various directive patterns', () => {
+    const input = `
+<div>
+    $foo(filter=(AAA))
+    $bar-2(except=(BBB))
+    $baz_3(filter=(CCC), except=(DDD))
+</div>`;
+
+    const expected = `
+<div>
+
+$foo(filter=AAA)
+
+$bar-2(except=BBB)
+
+$baz_3(filter=CCC, except=DDD)
+</div>`;
+
+    const result = migrations.reduce((text, migration) => migration(text), input);
+    expect(result).toBe(expected);
+  });
+});