v2 / vlib / x / markdown / extension.v
115 lines · 92 sloc · 3.12 KB · 5e4a634512f74766ee5732dbb33f1d4555b5cb0a
Raw
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.
4module 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.
9pub 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 |).
15pub struct TableExt {}
16
17// extend implements Extension for TableExt.
18pub fn (_ TableExt) extend(mut m Markdown) {
19 m.opts.tables = true
20}
21
22// StrikethroughExt adds GFM strikethrough support (~~text~~).
23pub struct StrikethroughExt {}
24
25// extend implements Extension for StrikethroughExt.
26pub fn (_ StrikethroughExt) extend(mut m Markdown) {
27 m.opts.strikethrough = true
28}
29
30// LinkifyExt adds autolink support for bare URLs and email addresses.
31pub struct LinkifyExt {}
32
33// extend implements Extension for LinkifyExt.
34pub fn (_ LinkifyExt) extend(mut m Markdown) {
35 m.opts.linkify = true
36}
37
38// TaskListExt adds GFM task list item support (- [ ] / - [x]).
39pub struct TaskListExt {}
40
41// extend implements Extension for TaskListExt.
42pub fn (_ TaskListExt) extend(mut m Markdown) {
43 m.opts.task_list = true
44}
45
46// FootnoteExt adds footnote support ([^label] references and [^label]: definitions).
47pub struct FootnoteExt {}
48
49// extend implements Extension for FootnoteExt.
50pub 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.
56pub struct TypographerExt {}
57
58// extend implements Extension for TypographerExt.
59pub fn (_ TypographerExt) extend(mut m Markdown) {
60 m.opts.typographer = true
61}
62
63// DefinitionListExt adds Pandoc-style definition list support.
64pub struct DefinitionListExt {}
65
66// extend implements Extension for DefinitionListExt.
67pub fn (_ DefinitionListExt) extend(mut m Markdown) {
68 m.opts.definition_list = true
69}
70
71// table returns a TableExt extension value.
72pub fn table() TableExt {
73 return TableExt{}
74}
75
76// strikethrough returns a StrikethroughExt extension value.
77pub fn strikethrough() StrikethroughExt {
78 return StrikethroughExt{}
79}
80
81// linkify returns a LinkifyExt extension value.
82pub fn linkify() LinkifyExt {
83 return LinkifyExt{}
84}
85
86// task_list returns a TaskListExt extension value.
87pub fn task_list() TaskListExt {
88 return TaskListExt{}
89}
90
91// footnote returns a FootnoteExt extension value.
92pub fn footnote() FootnoteExt {
93 return FootnoteExt{}
94}
95
96// typographer returns a TypographerExt extension value.
97pub fn typographer() TypographerExt {
98 return TypographerExt{}
99}
100
101// definition_list returns a DefinitionListExt extension value.
102pub fn definition_list() DefinitionListExt {
103 return DefinitionListExt{}
104}
105
106// gfm returns the core GitHub Flavored Markdown extensions:
107// TableExt, StrikethroughExt, LinkifyExt, and TaskListExt.
108pub fn gfm() []Extension {
109 return [
110 Extension(TableExt{}),
111 StrikethroughExt{},
112 LinkifyExt{},
113 TaskListExt{},
114 ]
115}
116