Просмотр исходного кода

feat: add def token in tokens and renderer (#3745)

* feat: add def token in tokens and renderer

* fix tests
Tony Brix 4 дней назад
Родитель
Сommit
07691248df
4 измененных файлов с 28 добавлено и 1 удалено
  1. 1 0
      src/Lexer.ts
  2. 5 1
      src/Parser.ts
  3. 4 0
      src/Renderer.ts
  4. 18 0
      test/unit/Lexer.test.js

+ 1 - 0
src/Lexer.ts

@@ -206,6 +206,7 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
             href: token.href,
             title: token.title,
           };
+          tokens.push(token);
         }
         continue;
       }

+ 5 - 1
src/Parser.ts

@@ -49,7 +49,7 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
       if (this.options.extensions?.renderers?.[anyToken.type]) {
         const genericToken = anyToken as Tokens.Generic;
         const ret = this.options.extensions.renderers[genericToken.type].call({ parser: this }, genericToken);
-        if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'paragraph', 'text'].includes(genericToken.type)) {
+        if (ret !== false || !['space', 'hr', 'heading', 'code', 'table', 'blockquote', 'list', 'html', 'def', 'paragraph', 'text'].includes(genericToken.type)) {
           out += ret || '';
           continue;
         }
@@ -90,6 +90,10 @@ export class _Parser<ParserOutput = string, RendererOutput = string> {
           out += this.renderer.html(token);
           continue;
         }
+        case 'def': {
+          out += this.renderer.def(token);
+          continue;
+        }
         case 'paragraph': {
           out += this.renderer.paragraph(token);
           continue;

+ 4 - 0
src/Renderer.ts

@@ -49,6 +49,10 @@ export class _Renderer<ParserOutput = string, RendererOutput = string> {
     return text as RendererOutput;
   }
 
+  def(token: Tokens.Def): RendererOutput {
+    return '' as RendererOutput;
+  }
+
   heading({ tokens, depth }: Tokens.Heading): RendererOutput {
     return `<h${depth}>${this.parser.parseInline(tokens)}</h${depth}>\n` as RendererOutput;
   }

+ 18 - 0
test/unit/Lexer.test.js

@@ -1331,6 +1331,15 @@ paragraph
         links: {
           link: { href: 'https://example.com', title: undefined },
         },
+        tokens: [
+          {
+            type: 'def',
+            raw: '[link]: https://example.com',
+            tag: 'link',
+            href: 'https://example.com',
+            title: undefined,
+          },
+        ],
       });
     });
 
@@ -1340,6 +1349,15 @@ paragraph
         links: {
           link: { href: 'https://example.com', title: 'title' },
         },
+        tokens: [
+          {
+            type: 'def',
+            raw: '[link]: https://example.com "title"',
+            tag: 'link',
+            href: 'https://example.com',
+            title: 'title',
+          },
+        ],
       });
     });
   });