aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/docs/src/plugins/remarkPosix2Windows.ts
blob: 66baca3066d064906b89fd272ab63300f39b6991 (plain) (blame)
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
/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 * Copyright (c) 2024 The Refinery Authors
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * This file is based on
 * https://github.com/facebook/docusaurus/blob/e4ecffe41878728acff55a8370bd7440706c02f7/packages/docusaurus-remark-plugin-npm2yarn/src/index.ts
 * but was changed to conver shell commands to POSIX to Windows syntax.
 */

import type { Code, Literal } from 'mdast';
import type { MdxjsEsm, MdxJsxFlowElement } from 'mdast-util-mdx';
import type { Transformer } from 'unified';
import type { Node, Parent } from 'unist';
import { visit } from 'unist-util-visit';

function isLiteral(node: Node): node is Literal {
  return node.type === 'mdxjsEsm';
}

function isTabImport(node: Node): boolean {
  return isLiteral(node) && node.value.includes('@theme/Tabs');
}

function isParent(node: Node): node is Parent {
  return 'children' in node && Array.isArray(node.children);
}

function isCode(node: Node): node is Code {
  return node.type === 'code';
}

function isPosix2Windows(node: Node): node is Code {
  return isCode(node) && node.meta === 'posix2windows';
}

function createTabItem(
  code: string,
  node: Code,
  value: string,
  label: string,
): MdxJsxFlowElement {
  return {
    type: 'mdxJsxFlowElement',
    name: 'TabItem',
    attributes: [
      {
        type: 'mdxJsxAttribute',
        name: 'value',
        value,
      },
      {
        type: 'mdxJsxAttribute',
        name: 'label',
        value: label,
      },
    ],
    children: [
      {
        type: node.type,
        lang: node.lang,
        value: code,
      },
    ],
  };
}

function transformNode(node: Code): MdxJsxFlowElement[] {
  const posixCode = node.value;
  const windowsCode = posixCode.replaceAll(/(?<=^\w*)\.\//gm, '.\\');
  return [
    {
      type: 'mdxJsxFlowElement',
      name: 'Tabs',
      attributes: [
        {
          type: 'mdxJsxAttribute',
          name: 'groupId',
          value: 'posix2windows',
        },
      ],
      children: [
        createTabItem(posixCode, node, 'posix', 'Linux or macOS'),
        createTabItem(windowsCode, node, 'windows', 'Windows (PowerShell)'),
      ],
    },
  ];
}

function createImportNode(): MdxjsEsm {
  return {
    type: 'mdxjsEsm',
    value:
      "import Tabs from '@theme/Tabs'\nimport TabItem from '@theme/TabItem'",
    data: {
      estree: {
        type: 'Program',
        body: [
          {
            type: 'ImportDeclaration',
            specifiers: [
              {
                type: 'ImportDefaultSpecifier',
                local: { type: 'Identifier', name: 'Tabs' },
              },
            ],
            source: {
              type: 'Literal',
              value: '@theme/Tabs',
              raw: "'@theme/Tabs'",
            },
          },
          {
            type: 'ImportDeclaration',
            specifiers: [
              {
                type: 'ImportDefaultSpecifier',
                local: { type: 'Identifier', name: 'TabItem' },
              },
            ],
            source: {
              type: 'Literal',
              value: '@theme/TabItem',
              raw: "'@theme/TabItem'",
            },
          },
        ],
        sourceType: 'module',
      },
    },
  };
}

export default function remarkPosix2Windows(): Transformer {
  return (root) => {
    let transformed = false;
    let alreadyImported = false;
    visit(root, (node) => {
      if (isTabImport(node)) {
        alreadyImported = true;
      }
      if (isParent(node)) {
        let index = 0;
        while (index < node.children.length) {
          const child = node.children[index];
          if (child !== undefined && isPosix2Windows(child)) {
            const result = transformNode(child);
            node.children.splice(index, 1, ...result);
            index += result.length;
            transformed = true;
          } else {
            index += 1;
          }
        }
      }
    });
    if (transformed && !alreadyImported) {
      if (isParent(root)) {
        root.children.unshift(createImportNode());
      } else {
        throw new Error("Cannot import '@theme/Tabs'");
      }
    }
  };
}