You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/3-packages/04-markdown.md
+11-11Lines changed: 11 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -197,7 +197,7 @@ Prepend `*` to a link's URI to open the link in a new tab:
197
197
198
198
## Adding custom features
199
199
200
-
`tempest/markdown` is meant to be extended. Adding custom parser rules is done in two steps: first you provide a `LexerRule`, this is a class that determines when your custom parsing logic should be triggered. Next you'll use a `Token` to render your selected Markdown code in any way you'd like.
200
+
`tempest/markdown` is meant to be extended. Adding custom parser rules is done in two steps: first you provide a `Rule`, this is a class that determines when your custom parsing logic should be triggered. Next you'll use a `Token` to render your selected Markdown code in any way you'd like.
201
201
202
202
Let's work with an example. Say you want to add support for including custom HTML snippets. It could look something like this:
203
203
@@ -207,32 +207,32 @@ Hello world
207
207
{{ snippets/call-to-action.html }}
208
208
```
209
209
210
-
The first step to adding this new feature is detecting when we run into our custom `{{ }}` syntax. This is done with a `LexerRule`. Let's call our implementation `SnippetRule`:
210
+
The first step to adding this new feature is detecting when we run into our custom `{{ }}` syntax. This is done with a `Rule`. Let's call our implementation `SnippetRule`:
211
211
212
212
```php
213
-
use Tempest\Markdown\Lexer;
213
+
use Tempest\Markdown\Parser;
214
214
use Tempest\Markdown\Rule;
215
215
use Tempest\Markdown\Token;
216
216
217
217
final readonly class SnippetRule implements Rule
218
218
{
219
-
public function shouldLex(Lexer $lexer): bool
219
+
public function shouldLex(Parser $parser): bool
220
220
{
221
221
// Our rule takes effect as soon as we run into `{{`
222
222
223
-
return $lexer->comesNext('{{', length: 2);
223
+
return $parser->comesNext('{{', length: 2);
224
224
}
225
225
226
-
public function lex(Lexer $lexer): ?Token
226
+
public function lex(Parser $parser): ?Token
227
227
{
228
228
// We'll consume all { characters
229
-
$lexer->consumeWhile('{');
229
+
$parser->consumeWhile('{');
230
230
231
231
// Then we'll consume and store the snippet path itself until we encounter the closing } characters
232
-
$snippet = $lexer->consumeUntil('}');
232
+
$snippet = $parser->consumeUntil('}');
233
233
234
234
// Then we'll consume the closing } characters
235
-
$lexer->consumeWhile('}');
235
+
$parser->consumeWhile('}');
236
236
237
237
// Finally, we return a token with the snippet
238
238
return new SnippetToken(trim($snippet));
@@ -241,7 +241,7 @@ final readonly class SnippetRule implements Rule
241
241
```
242
242
243
243
:::info
244
-
For performance reasons, it's best to explcitly add the `{:hl-property:length:}` parameter to the `{:hl-property:comesNext:}` call. It's not required, but it will make parsing faster. If possible, also try not to rely on regex within your lexer rules, as it may become a performance bottleneck.
244
+
For performance reasons, it's best to explcitly add the `{:hl-property:length:}` parameter to the `{:hl-property:comesNext:}` call. It's not required, but it will make parsing faster. If possible, also try not to rely on regex within your parser rules, as it may become a performance bottleneck.
245
245
:::
246
246
247
247
So that's our rule implementation: we consumed our custom `{{ path }}` syntax, and created a token with that path. Let's take a look at the token implementation next.
@@ -302,7 +302,7 @@ $markdown = new Markdown()->withRules(
302
302
);
303
303
```
304
304
305
-
This way you have full control over how the parser works. For more inspiration, you can look at the [rules that come built-in with the package](https://github.com/tempestphp/markdown/tree/main/src/LexerRules).
305
+
This way you have full control over how the parser works. For more inspiration, you can look at the [rules that come built-in with the package](https://github.com/tempestphp/markdown/tree/main/src/Rules).
0 commit comments