Debian Codegraph @

markup/converter/hooks/hooks.go

raw
1 // Copyright 2019 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package hooks
15
16 import (
17 "context"
18 "io"
19
20 "github.com/gohugoio/hugo/common/hugio"
21 "github.com/gohugoio/hugo/common/text"
22 "github.com/gohugoio/hugo/common/types/hstring"
23 "github.com/gohugoio/hugo/markup/internal/attributes"
24 )
25
26 var _ AttributesOptionsSliceProvider = (*attributes.AttributesHolder)(nil)
27
28 type AttributesProvider interface {
29 // Attributes passed in from Markdown (e.g. { attrName1=attrValue1 attrName2="attr Value 2" }).
30 Attributes() map[string]any
31 }
32
33 // LinkContext is the context passed to a link render hook.
34 type LinkContext interface {
35 PageProvider
36
37 // The link URL.
38 Destination() string
39
40 // The link title attribute.
41 Title() string
42
43 // The rendered (HTML) text.
44 Text() hstring.HTML
45
46 // The plain variant of Text.
47 PlainText() string
48 }
49
50 // ImageLinkContext is the context passed to a image link render hook.
51 type ImageLinkContext interface {
52 LinkContext
53
54 // Returns true if this is a standalone image and the config option
55 // markup.goldmark.parser.wrapStandAloneImageWithinParagraph is disabled.
56 IsBlock() bool
57
58 // Zero-based ordinal for all the images in the current document.
59 Ordinal() int
60 }
61
62 // CodeblockContext is the context passed to a code block render hook.
63 type CodeblockContext interface {
64 BaseContext
65 AttributesProvider
66
67 // Chroma highlighting processing options. This will only be filled if Type is a known Chroma Lexer.
68 Options() map[string]any
69
70 // The type of code block. This will be the programming language, e.g. bash, when doing code highlighting.
71 Type() string
72
73 // The text between the code fences.
74 Inner() string
75 }
76
77 // TableContext is the context passed to a table render hook.
78 type TableContext interface {
79 BaseContext
80 AttributesProvider
81
82 THead() []TableRow
83 TBody() []TableRow
84 }
85
86 // BaseContext is the base context used in most render hooks.
87 type BaseContext interface {
88 text.Positioner
89 PageProvider
90
91 // Zero-based ordinal for all elements of this kind in the current document.
92 Ordinal() int
93 }
94
95 // BlockquoteContext is the context passed to a blockquote render hook.
96 type BlockquoteContext interface {
97 BaseContext
98
99 AttributesProvider
100
101 // The blockquote text.
102 // If type is "alert", this will be the alert text.
103 Text() hstring.HTML
104
105 /// Returns the blockquote type, one of "regular" and "alert".
106 // Type "alert" indicates that this is a GitHub type alert.
107 Type() string
108
109 // The GitHub alert type converted to lowercase, e.g. "note".
110 // Only set if Type is "alert".
111 AlertType() string
112
113 // The alert title.
114 // Currently only relevant for Obsidian alerts.
115 // GitHub does not suport alert titles and will not render alerts with titles.
116 AlertTitle() hstring.HTML
117
118 // The alert sign, "+" or "-" or "" used to indicate folding.
119 // Currently only relevant for Obsidian alerts.
120 // GitHub does not suport alert signs and will not render alerts with signs.
121 AlertSign() string
122 }
123
124 // PassThroughContext is the context passed to a passthrough render hook.
125 type PassthroughContext interface {
126 BaseContext
127 AttributesProvider
128
129 // Currently one of "inline" or "block".
130 Type() string
131
132 // The inner content of the passthrough element, excluding the delimiters.
133 Inner() string
134 }
135
136 type AttributesOptionsSliceProvider interface {
137 AttributesSlice() []attributes.Attribute
138 OptionsSlice() []attributes.Attribute
139 }
140
141 type LinkRenderer interface {
142 RenderLink(cctx context.Context, w io.Writer, ctx LinkContext) error
143 }
144
145 type CodeBlockRenderer interface {
146 RenderCodeblock(cctx context.Context, w hugio.FlexiWriter, ctx CodeblockContext) error
147 }
148
149 type BlockquoteRenderer interface {
150 RenderBlockquote(cctx context.Context, w hugio.FlexiWriter, ctx BlockquoteContext) error
151 }
152
153 type TableRenderer interface {
154 RenderTable(cctx context.Context, w hugio.FlexiWriter, ctx TableContext) error
155 }
156
157 type PassthroughRenderer interface {
158 RenderPassthrough(cctx context.Context, w io.Writer, ctx PassthroughContext) error
159 }
160
161 type IsDefaultCodeBlockRendererProvider interface {
162 IsDefaultCodeBlockRenderer() bool
163 }
164
165 // HeadingContext contains accessors to all attributes that a HeadingRenderer
166 // can use to render a heading.
167 type HeadingContext interface {
168 PageProvider
169 // Level is the level of the header (i.e. 1 for top-level, 2 for sub-level, etc.).
170 Level() int
171 // Anchor is the HTML id assigned to the heading.
172 Anchor() string
173 // Text is the rendered (HTML) heading text, excluding the heading marker.
174 Text() hstring.HTML
175 // PlainText is the unrendered version of Text.
176 PlainText() string
177
178 // Attributes (e.g. CSS classes)
179 AttributesProvider
180 }
181
182 type PageProvider interface {
183 // Page is the page being rendered.
184 Page() any
185
186 // PageInner may be different than Page when .RenderShortcodes is in play.
187 // The main use case for this is to include other pages' markdown into the current page
188 // but resolve resources and pages relative to the original.
189 PageInner() any
190 }
191
192 // HeadingRenderer describes a uniquely identifiable rendering hook.
193 type HeadingRenderer interface {
194 // RenderHeading writes the rendered content to w using the data in w.
195 RenderHeading(cctx context.Context, w io.Writer, ctx HeadingContext) error
196 }
197
198 // ElementPositionResolver provides a way to resolve the start Position
199 // of a markdown element in the original source document.
200 // This may be both slow and approximate, so should only be
201 // used for error logging.
202 type ElementPositionResolver interface {
203 // ResolvePosition returns the position of the element in the original source document, -1 if it's not defined.
204 ResolvePosition(renderContext any, srcPos int) text.Position
205 }
206
207 type RendererType int
208
209 const (
210 LinkRendererType RendererType = iota + 1
211 ImageRendererType
212 HeadingRendererType
213 CodeBlockRendererType
214 PassthroughRendererType
215 BlockquoteRendererType
216 TableRendererType
217 )
218
219 type GetRendererFunc func(t RendererType, id any) any
220
221 type TableCell struct {
222 Text hstring.HTML
223 Alignment string // left, center, or right
224 }
225
226 type TableRow []TableCell
227
228 type Table struct {
229 THead []TableRow
230 TBody []TableRow
231 }