| 1 | // Copyright 2026 The V Language. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module markdown |
| 5 | |
| 6 | // Extension is the interface implemented by markdown extensions. |
| 7 | // An extension configures the Markdown processor by enabling parser and |
| 8 | // renderer features. |
| 9 | pub interface Extension { |
| 10 | // extend is called once when the extension is registered with a Markdown processor. |
| 11 | extend(mut m Markdown) |
| 12 | } |
| 13 | |
| 14 | // TableExt adds GitHub Flavored Markdown table support (| col | col |). |
| 15 | pub struct TableExt {} |
| 16 | |
| 17 | // extend implements Extension for TableExt. |
| 18 | pub fn (_ TableExt) extend(mut m Markdown) { |
| 19 | m.opts.tables = true |
| 20 | } |
| 21 | |
| 22 | // StrikethroughExt adds GFM strikethrough support (~~text~~). |
| 23 | pub struct StrikethroughExt {} |
| 24 | |
| 25 | // extend implements Extension for StrikethroughExt. |
| 26 | pub fn (_ StrikethroughExt) extend(mut m Markdown) { |
| 27 | m.opts.strikethrough = true |
| 28 | } |
| 29 | |
| 30 | // LinkifyExt adds autolink support for bare URLs and email addresses. |
| 31 | pub struct LinkifyExt {} |
| 32 | |
| 33 | // extend implements Extension for LinkifyExt. |
| 34 | pub fn (_ LinkifyExt) extend(mut m Markdown) { |
| 35 | m.opts.linkify = true |
| 36 | } |
| 37 | |
| 38 | // TaskListExt adds GFM task list item support (- [ ] / - [x]). |
| 39 | pub struct TaskListExt {} |
| 40 | |
| 41 | // extend implements Extension for TaskListExt. |
| 42 | pub fn (_ TaskListExt) extend(mut m Markdown) { |
| 43 | m.opts.task_list = true |
| 44 | } |
| 45 | |
| 46 | // FootnoteExt adds footnote support ([^label] references and [^label]: definitions). |
| 47 | pub struct FootnoteExt {} |
| 48 | |
| 49 | // extend implements Extension for FootnoteExt. |
| 50 | pub fn (_ FootnoteExt) extend(mut m Markdown) { |
| 51 | m.opts.footnotes = true |
| 52 | } |
| 53 | |
| 54 | // TypographerExt replaces ASCII punctuation sequences with Unicode typographic |
| 55 | // equivalents: -- en dash, --- em dash, ... ellipsis, and smart quotes. |
| 56 | pub struct TypographerExt {} |
| 57 | |
| 58 | // extend implements Extension for TypographerExt. |
| 59 | pub fn (_ TypographerExt) extend(mut m Markdown) { |
| 60 | m.opts.typographer = true |
| 61 | } |
| 62 | |
| 63 | // DefinitionListExt adds Pandoc-style definition list support. |
| 64 | pub struct DefinitionListExt {} |
| 65 | |
| 66 | // extend implements Extension for DefinitionListExt. |
| 67 | pub fn (_ DefinitionListExt) extend(mut m Markdown) { |
| 68 | m.opts.definition_list = true |
| 69 | } |
| 70 | |
| 71 | // table returns a TableExt extension value. |
| 72 | pub fn table() TableExt { |
| 73 | return TableExt{} |
| 74 | } |
| 75 | |
| 76 | // strikethrough returns a StrikethroughExt extension value. |
| 77 | pub fn strikethrough() StrikethroughExt { |
| 78 | return StrikethroughExt{} |
| 79 | } |
| 80 | |
| 81 | // linkify returns a LinkifyExt extension value. |
| 82 | pub fn linkify() LinkifyExt { |
| 83 | return LinkifyExt{} |
| 84 | } |
| 85 | |
| 86 | // task_list returns a TaskListExt extension value. |
| 87 | pub fn task_list() TaskListExt { |
| 88 | return TaskListExt{} |
| 89 | } |
| 90 | |
| 91 | // footnote returns a FootnoteExt extension value. |
| 92 | pub fn footnote() FootnoteExt { |
| 93 | return FootnoteExt{} |
| 94 | } |
| 95 | |
| 96 | // typographer returns a TypographerExt extension value. |
| 97 | pub fn typographer() TypographerExt { |
| 98 | return TypographerExt{} |
| 99 | } |
| 100 | |
| 101 | // definition_list returns a DefinitionListExt extension value. |
| 102 | pub fn definition_list() DefinitionListExt { |
| 103 | return DefinitionListExt{} |
| 104 | } |
| 105 | |
| 106 | // gfm returns the core GitHub Flavored Markdown extensions: |
| 107 | // TableExt, StrikethroughExt, LinkifyExt, and TaskListExt. |
| 108 | pub fn gfm() []Extension { |
| 109 | return [ |
| 110 | Extension(TableExt{}), |
| 111 | StrikethroughExt{}, |
| 112 | LinkifyExt{}, |
| 113 | TaskListExt{}, |
| 114 | ] |
| 115 | } |
| 116 | |