Ver Fonte

WIP: add remark-growi-directive

Yuki Takei há 1 ano atrás
pai
commit
dc93d38cfe

+ 2 - 1
bin/data-migrations/src/migrations/v60x/index.js

@@ -2,6 +2,7 @@ const bracketlink = require('./bracketlink');
 const csv = require('./csv');
 const csv = require('./csv');
 const drawio = require('./drawio');
 const drawio = require('./drawio');
 const plantUML = require('./plantuml');
 const plantUML = require('./plantuml');
+const remarkGrowiDirective = require('./remark-growi-directive');
 const tsv = require('./tsv');
 const tsv = require('./tsv');
 
 
-module.exports = [...bracketlink, ...csv, ...drawio, ...plantUML, ...tsv];
+module.exports = [...bracketlink, ...csv, ...drawio, ...plantUML, ...tsv, ...remarkGrowiDirective];

+ 23 - 0
bin/data-migrations/src/migrations/v60x/remark-growi-directive/README.ja.md

@@ -0,0 +1,23 @@
+# remark-growi-directive
+
+以下の要領で replace する
+
+## 1. HTMLタグ内で `$lsx()` を利用している箇所
+- 置換対象文章の詳細
+  - `$lsx()`がHTMLタグ内かつ、当該`$lsx()`記述の1行前が空行ではない場合に1行前に空行を挿入
+  - `$lsx()`がHTMLタグ内かつ、`$lsx()`記述行の行頭にインデントがついている場合に当該行のインデントを削除
+  - `$lsx()`がHTMLタグ内かつ、当該`$lsx()`記述の1行後のHTMLタグ記述行にインデントがついている場合にその行頭のインデントを削除
+
+## 2. `$lsx()` を利用している箇所
+- 置換対象文章の詳細
+  - `$lsx()`の引数内で括弧 `()` を使用している場合、括弧を削除
+    - before: `$lsx()`(depth=2, filter=(業務課))
+    - after: `$lsx()`(depth=2, filter=業務課)
+
+## テストについて
+
+以下を満たす
+
+- input が `example.md` のとき、`example-expected.md` を出力する
+- input が `example-expected.md` のとき、`example-expected.md` を出力する (変更が起こらない)
+

+ 1 - 0
bin/data-migrations/src/migrations/v60x/remark-growi-directive/index.js

@@ -0,0 +1 @@
+module.exports = require('./remark-growi-directive');

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

@@ -0,0 +1,52 @@
+/**
+ * @typedef {import('../../../types').MigrationModule} MigrationModule
+ */
+
+module.exports = [
+  /**
+   * Adjust line breaks and indentation for $lsx() within HTML tags
+   * @type {MigrationModule}
+   */
+  (body) => {
+    // Split into lines for better processing
+    const lines = body.split('\n');
+
+    for (let i = 0; i < lines.length; i++) {
+      // Find lines containing $lsx()
+      if (lines[i].includes('$lsx(')) {
+        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
+        }
+
+        // If next line contains an HTML tag
+        if (nextLine.includes('</')) {
+          // Handle next line (remove indentation)
+          lines[i + 1] = nextLine.trimStart();
+        }
+      }
+    }
+
+    return lines.join('\n');
+  },
+
+  /**
+   * Remove unnecessary parentheses in $lsx() filter arguments
+   * @type {MigrationModule}
+   */
+  (body) => {
+    return body.replace(/\$lsx\([^)]*\)/g, (match) => {
+      // Find and remove parentheses in filter=(...) pattern
+      return match.replace(/filter=\(([^)]+)\)/g, 'filter=$1');
+    });
+  },
+];

+ 9 - 0
bin/vitest.config.ts

@@ -0,0 +1,9 @@
+import { defineConfig } from 'vitest/config';
+
+export default defineConfig({
+  test: {
+    environment: 'node',
+    clearMocks: true,
+    globals: true,
+  },
+});

+ 1 - 0
vitest.workspace.mts

@@ -1,6 +1,7 @@
 export default [
 export default [
   'apps/*/vitest.config.ts',
   'apps/*/vitest.config.ts',
   'apps/*/vitest.workspace.ts',
   'apps/*/vitest.workspace.ts',
+  'bin/vitest.config.ts',
   'packages/*/vitest.config.ts',
   'packages/*/vitest.config.ts',
   'packages/*/vitest.workspace.ts',
   'packages/*/vitest.workspace.ts',
 ];
 ];