|
| 1 | +# Adding Languages |
| 2 | + |
| 3 | +To add a language to Code Explorer, follow these steps. |
| 4 | + |
| 5 | +## Step 1: Add a logo |
| 6 | + |
| 7 | +Add an SVG logo into the `public/languages/` directory. |
| 8 | + |
| 9 | +## Step 2: Update `src/hooks/use-explorer.ts` |
| 10 | + |
| 11 | +1. Update the `Language` type to contain an entry for the new language. |
| 12 | +1. Create a new mode variable that defines the primary parsing mode (such as `jsonMode` or `cssMode`). |
| 13 | +1. Update the `Code` type to include an entry for the new language. |
| 14 | +1. Create a new options variable that defines the options for the new language (such as `JsonOptions` or `CssOptions`). |
| 15 | +1. Update the `ExplorerState` type to include getters and setters for the new language's language options. |
| 16 | +1. Update the `useExplorer` variable to include the default options for the new language. |
| 17 | + |
| 18 | +## Step 3: Update `src/lib/const.ts` |
| 19 | + |
| 20 | +1. Import the type for the new language options from `src/hooks/use-explorer.ts`. |
| 21 | +1. Add an entry in the `languages` variable for the new language. |
| 22 | +1. Export a new variable describing the available modes for the new language (such as `jsonModes` or `cssModes`). |
| 23 | +1. Default the default code for the new language (such as `defaultJsonCode` or `defaultCssCode`). |
| 24 | +1. Add an entry in the `defaultCode` variable for the new language's default code. |
| 25 | +1. Export a variable containing the default options for the new language (such as `defaultJsonOptions` or `defaultCssOptions`). |
| 26 | + |
| 27 | +## Step 4: Update `src/hooks/use-explorer.ts` (yes, again) |
| 28 | + |
| 29 | +Now import the default options for the new language from `src/lib/const.ts`. |
| 30 | + |
| 31 | +## Step 5: Update `src/components/options.tsx` |
| 32 | + |
| 33 | +1. Import the new language mode type from `src/hooks/use-explorer` (such as `JsonMode` or `CssMode`). |
| 34 | +1. Import the available modes for the new language from `src/hooks/const` (such as `jsonModes` or `cssModes`). |
| 35 | +1. Create an options panel for the new language (such as `JsonPanel` or `CssPanel`). |
| 36 | +1. Update the `Panel` variable to use the new options panel. |
| 37 | + |
| 38 | +## Step 6: Update `src/components/editor.ts` |
| 39 | + |
| 40 | +1. Install the appropriate CodeMirror plugin for the new language. |
| 41 | +1. Update the `languageExtensions` variable to include the new language CodeMirror plugin. |
| 42 | + |
| 43 | +## Step 7: Add AST components |
| 44 | + |
| 45 | +In the `src/components/ast` directory, create two files: |
| 46 | + |
| 47 | +1. `{new language}-ast.tsx` |
| 48 | +1. `{new language}-ast-tree-item.tsx` |
| 49 | + |
| 50 | +Replace `{new language}` with the name of the language. You can copy existing files for other languages to get started. |
| 51 | + |
| 52 | +In `{new language}-ast-tree-item.tsx`, update the name of the options type and the exported component to match the new language. |
| 53 | + |
| 54 | +Next, install the ESLint language plugin for the new language. |
| 55 | + |
| 56 | +In `{new language}-ast.tsx`: |
| 57 | + |
| 58 | +1. Update language references to point to the new language. |
| 59 | +1. Import the ESLint language plugin to parse the code. |
| 60 | +1. Set the `defaultValue` in the accordion to the name of the root node for the new language AST. |
| 61 | + |
| 62 | +Last, update `src/components/index.tsx` to import `{new language}-ast.tsx` and update `Ast` to include the new language. |
0 commit comments