Skip to content

Commit 41edce2

Browse files
doc: add language injection
1 parent 1e1d521 commit 41edce2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

website/reference/sgconfig.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,58 @@ customLanguages:
140140
libraryPath: mojo.so # path to dynamic library
141141
extensions: [mojo, 🔥] # file extensions for this language
142142
expandoChar: _ # optional char to replace $ in your pattern
143+
```
144+
145+
## `languageInjections` <Badge type="warning" text="Experimental" />
146+
* type: `List<LanguageInjection>`
147+
* required: No
148+
* status: **Experimental**
149+
150+
A list of language injections to support embedded languages in the project like JS/CSS in HTML.
151+
This is an experimental feature.
152+
153+
Please see the [guide](/advanced/language-injection.html) for detailed instructions.
154+
155+
A language injection object has the following options.
156+
157+
### `hostLanguage`
158+
* type: `String`
159+
* required: Yes
160+
161+
The host language name, e.g. `html`. This is the language of documents that contains the embedded language code.
162+
163+
### `rule`
164+
* type: `Rule` object
165+
* required: Yes
166+
167+
Defines the ast-grep rule to identify the injected language region within the host language documents.
168+
169+
### `injected`
170+
* type: `String` or `List<String>`
171+
* required: Yes
172+
173+
The injected language name, e.g. `js`. This is the language of the embedded code.
174+
175+
It can be a static string or a list of strings. If it is a list, ast-grep will use the `$LANG` meta variable captured in the rule to dynamically determine the injected language. The list of strings is the candidate language names to match the `$LANG` meta variable.
176+
177+
**Example:**
178+
179+
This is a configuration to support styled-components in JS files with static `injected` language.
180+
181+
```yaml
182+
languageInjections:
183+
- hostLanguage: js
184+
rule:
185+
pattern: styled.$TAG`$CONTENT`
186+
injected: css
187+
```
188+
189+
This is a configuration to support CSS in JS style in JS files with dynamic `injected` language.
190+
191+
```yaml
192+
languageInjections:
193+
- hostLanguage: js
194+
rule:
195+
pattern: styled.$LANG`$CONTENT`
196+
injected: [css, scss, less]
143197
```

0 commit comments

Comments
 (0)