1package extension
2
3import (
4 "bytes"
5 "fmt"
6 "regexp"
7
8 "github.com/yuin/goldmark"
9 gast "github.com/yuin/goldmark/ast"
10 "github.com/yuin/goldmark/extension/ast"
11 "github.com/yuin/goldmark/parser"
12 "github.com/yuin/goldmark/renderer"
13 "github.com/yuin/goldmark/renderer/html"
14 "github.com/yuin/goldmark/text"
15 "github.com/yuin/goldmark/util"
16)
17
18var escapedPipeCellListKey = parser.NewContextKey()
19
20type escapedPipeCell struct {
21 Cell *ast.TableCell
22 Pos []int
23 Transformed bool
24}
25
26// TableCellAlignMethod indicates how are table cells aligned in HTML format.
27type TableCellAlignMethod int
28
29const (
30 // TableCellAlignDefault renders alignments by default method.
31 // With XHTML, alignments are rendered as an align attribute.
32 // With HTML5, alignments are rendered as a style attribute.
33 TableCellAlignDefault TableCellAlignMethod = iota
34
35 // TableCellAlignAttribute renders alignments as an align attribute.
36 TableCellAlignAttribute
37
38 // TableCellAlignStyle renders alignments as a style attribute.
39 TableCellAlignStyle
40
41 // TableCellAlignNone does not care about alignments.
42 // If you using classes or other styles, you can add these attributes
43 // in an ASTTransformer.
44 TableCellAlignNone
45)
46
47// TableConfig struct holds options for the extension.
48type TableConfig struct {
49 html.Config
50
51 // TableCellAlignMethod indicates how are table celss aligned.
52 TableCellAlignMethod TableCellAlignMethod
53}
54
55// TableOption interface is a functional option interface for the extension.
56type TableOption interface {
57 renderer.Option
58 // SetTableOption sets given option to the extension.
59 SetTableOption(*TableConfig)
60}
61
62// NewTableConfig returns a new Config with defaults.
63func NewTableConfig() TableConfig {
64 return TableConfig{
65 Config: html.NewConfig(),
66 TableCellAlignMethod: TableCellAlignDefault,
67 }
68}
69
70// SetOption implements renderer.SetOptioner.
71func (c *TableConfig) SetOption(name renderer.OptionName, value any) {
72 switch name {
73 case optTableCellAlignMethod:
74 c.TableCellAlignMethod = value.(TableCellAlignMethod)
75 default:
76 c.Config.SetOption(name, value)
77 }
78}
79
80type withTableHTMLOptions struct {
81 value []html.Option
82}
83
84func (o *withTableHTMLOptions) SetConfig(c *renderer.Config) {
85 if o.value != nil {
86 for _, v := range o.value {
87 v.(renderer.Option).SetConfig(c)
88 }
89 }
90}
91
92func (o *withTableHTMLOptions) SetTableOption(c *TableConfig) {
93 if o.value != nil {
94 for _, v := range o.value {
95 v.SetHTMLOption(&c.Config)
96 }
97 }
98}
99
100// WithTableHTMLOptions is functional option that wraps goldmark HTMLRenderer options.
101func WithTableHTMLOptions(opts ...html.Option) TableOption {
102 return &withTableHTMLOptions{opts}
103}
104
105const optTableCellAlignMethod renderer.OptionName = "TableTableCellAlignMethod"
106
107type withTableCellAlignMethod struct {
108 value TableCellAlignMethod
109}
110
111func (o *withTableCellAlignMethod) SetConfig(c *renderer.Config) {
112 c.Options[optTableCellAlignMethod] = o.value
113}
114
115func (o *withTableCellAlignMethod) SetTableOption(c *TableConfig) {
116 c.TableCellAlignMethod = o.value
117}
118
119// WithTableCellAlignMethod is a functional option that indicates how are table cells aligned in HTML format.
120func WithTableCellAlignMethod(a TableCellAlignMethod) TableOption {
121 return &withTableCellAlignMethod{a}
122}
123
124func isTableDelim(bs []byte) bool {
125 if w, _ := util.IndentWidth(bs, 0); w > 3 {
126 return false
127 }
128 allSep := true
129 for _, b := range bs {
130 if b != '-' {
131 allSep = false
132 }
133 if !(util.IsSpace(b) || b == '-' || b == '|' || b == ':') {
134 return false
135 }
136 }
137 return !allSep
138}
139
140var tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`)
141var tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`)
142var tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`)
143var tableDelimNone = regexp.MustCompile(`^\s*\-+\s*$`)
144
145type tableParagraphTransformer struct {
146}
147
148var defaultTableParagraphTransformer = &tableParagraphTransformer{}
149
150// NewTableParagraphTransformer returns a new ParagraphTransformer
151// that can transform paragraphs into tables.
152func NewTableParagraphTransformer() parser.ParagraphTransformer {
153 return defaultTableParagraphTransformer
154}
155
156func (b *tableParagraphTransformer) Transform(node *gast.Paragraph, reader text.Reader, pc parser.Context) {
157 ppos := node.Pos()
158 lines := node.Lines()
159 if lines.Len() < 2 {
160 return
161 }
162 for i := 1; i < lines.Len(); i++ {
163 alignments := b.parseDelimiter(lines.At(i), reader)
164 if alignments == nil {
165 continue
166 }
167 header := b.parseRow(lines.At(i-1), alignments, true, reader, pc)
168 if header == nil || len(alignments) != header.ChildCount() {
169 return
170 }
171 table := ast.NewTable()
172 table.Alignments = alignments
173 table.SetPos(ppos)
174 table.AppendChild(table, ast.NewTableHeader(header))
175 for j := i + 1; j < lines.Len(); j++ {
176 table.AppendChild(table, b.parseRow(lines.At(j), alignments, false, reader, pc))
177 }
178 node.Lines().SetSliced(0, i-1)
179 node.Parent().InsertAfter(node.Parent(), node, table)
180 if node.Lines().Len() == 0 {
181 node.Parent().RemoveChild(node.Parent(), node)
182 } else {
183 last := node.Lines().At(i - 2)
184 last.Stop = last.Stop - 1 // trim last newline(\n)
185 node.Lines().Set(i-2, last)
186 }
187 }
188}
189
190func (b *tableParagraphTransformer) parseRow(segment text.Segment,
191 alignments []ast.Alignment, isHeader bool, reader text.Reader, pc parser.Context) *ast.TableRow {
192 npos := segment
193 source := reader.Source()
194 segment = segment.TrimLeftSpace(source)
195 segment = segment.TrimRightSpace(source)
196 line := segment.Value(source)
197 pos := 0
198 limit := len(line)
199 row := ast.NewTableRow(alignments)
200 row.SetPos(npos.Start)
201 if len(line) > 0 && line[pos] == '|' {
202 pos++
203 }
204 if len(line) > 0 && line[limit-1] == '|' {
205 limit--
206 }
207 i := 0
208 for ; pos < limit; i++ {
209 alignment := ast.AlignNone
210 if i >= len(alignments) {
211 if !isHeader {
212 return row
213 }
214 } else {
215 alignment = alignments[i]
216 }
217
218 var escapedCell *escapedPipeCell
219 node := ast.NewTableCell()
220 node.SetPos(npos.Start + pos - npos.Padding)
221 node.Alignment = alignment
222 hasBacktick := false
223 closure := pos
224 for ; closure < limit; closure++ {
225 if line[closure] == '`' {
226 hasBacktick = true
227 }
228 if line[closure] == '|' {
229 if closure == 0 || line[closure-1] != '\\' {
230 break
231 } else if hasBacktick {
232 if escapedCell == nil {
233 escapedCell = &escapedPipeCell{node, []int{}, false}
234 escapedList := pc.ComputeIfAbsent(escapedPipeCellListKey,
235 func() any {
236 return []*escapedPipeCell{}
237 }).([]*escapedPipeCell)
238 escapedList = append(escapedList, escapedCell)
239 pc.Set(escapedPipeCellListKey, escapedList)
240 }
241 escapedCell.Pos = append(escapedCell.Pos, segment.Start+closure-1)
242 }
243 }
244 }
245 seg := text.NewSegment(segment.Start+pos, segment.Start+closure)
246 seg = seg.TrimLeftSpace(source)
247 seg = seg.TrimRightSpace(source)
248 node.Lines().Append(seg)
249 row.AppendChild(row, node)
250 pos = closure + 1
251 }
252 for ; i < len(alignments); i++ {
253 row.AppendChild(row, ast.NewTableCell())
254 }
255 return row
256}
257
258func (b *tableParagraphTransformer) parseDelimiter(segment text.Segment, reader text.Reader) []ast.Alignment {
259
260 line := segment.Value(reader.Source())
261 if !isTableDelim(line) {
262 return nil
263 }
264 cols := bytes.Split(line, []byte{'|'})
265 if util.IsBlank(cols[0]) {
266 cols = cols[1:]
267 }
268 if len(cols) > 0 && util.IsBlank(cols[len(cols)-1]) {
269 cols = cols[:len(cols)-1]
270 }
271
272 var alignments []ast.Alignment
273 for _, col := range cols {
274 if tableDelimLeft.Match(col) {
275 alignments = append(alignments, ast.AlignLeft)
276 } else if tableDelimRight.Match(col) {
277 alignments = append(alignments, ast.AlignRight)
278 } else if tableDelimCenter.Match(col) {
279 alignments = append(alignments, ast.AlignCenter)
280 } else if tableDelimNone.Match(col) {
281 alignments = append(alignments, ast.AlignNone)
282 } else {
283 return nil
284 }
285 }
286 return alignments
287}
288
289type tableASTTransformer struct {
290}
291
292var defaultTableASTTransformer = &tableASTTransformer{}
293
294// NewTableASTTransformer returns a parser.ASTTransformer for tables.
295func NewTableASTTransformer() parser.ASTTransformer {
296 return defaultTableASTTransformer
297}
298
299func (a *tableASTTransformer) Transform(node *gast.Document, reader text.Reader, pc parser.Context) {
300 lst := pc.Get(escapedPipeCellListKey)
301 if lst == nil {
302 return
303 }
304 pc.Set(escapedPipeCellListKey, nil)
305 for _, v := range lst.([]*escapedPipeCell) {
306 if v.Transformed {
307 continue
308 }
309 _ = gast.Walk(v.Cell, func(n gast.Node, entering bool) (gast.WalkStatus, error) {
310 if !entering || n.Kind() != gast.KindCodeSpan {
311 return gast.WalkContinue, nil
312 }
313
314 for c := n.FirstChild(); c != nil; {
315 next := c.NextSibling()
316 if c.Kind() != gast.KindText {
317 c = next
318 continue
319 }
320 parent := c.Parent()
321 ts := &c.(*gast.Text).Segment
322 n := c
323 for _, v := range lst.([]*escapedPipeCell) {
324 for _, pos := range v.Pos {
325 if ts.Start <= pos && pos < ts.Stop {
326 segment := n.(*gast.Text).Segment
327 n1 := gast.NewRawTextSegment(segment.WithStop(pos))
328 n2 := gast.NewRawTextSegment(segment.WithStart(pos + 1))
329 parent.InsertAfter(parent, n, n1)
330 parent.InsertAfter(parent, n1, n2)
331 parent.RemoveChild(parent, n)
332 n = n2
333 v.Transformed = true
334 }
335 }
336 }
337 c = next
338 }
339 return gast.WalkContinue, nil
340 })
341 }
342}
343
344// TableHTMLRenderer is a renderer.NodeRenderer implementation that
345// renders Table nodes.
346type TableHTMLRenderer struct {
347 TableConfig
348}
349
350// NewTableHTMLRenderer returns a new TableHTMLRenderer.
351func NewTableHTMLRenderer(opts ...TableOption) renderer.NodeRenderer {
352 r := &TableHTMLRenderer{
353 TableConfig: NewTableConfig(),
354 }
355 for _, opt := range opts {
356 opt.SetTableOption(&r.TableConfig)
357 }
358 return r
359}
360
361// RegisterFuncs implements renderer.NodeRenderer.RegisterFuncs.
362func (r *TableHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
363 reg.Register(ast.KindTable, r.renderTable)
364 reg.Register(ast.KindTableHeader, r.renderTableHeader)
365 reg.Register(ast.KindTableRow, r.renderTableRow)
366 reg.Register(ast.KindTableCell, r.renderTableCell)
367}
368
369// TableAttributeFilter defines attribute names which table elements can have.
370//
371// - align: Deprecated
372// - bgcolor: Deprecated
373// - border: Deprecated
374// - cellpadding: Deprecated
375// - cellspacing: Deprecated
376// - frame: Deprecated
377// - rules: Deprecated
378// - summary: Deprecated
379// - width: Deprecated.
380var TableAttributeFilter = html.GlobalAttributeFilter.ExtendString(`align,bgcolor,border,cellpadding,cellspacing,frame,rules,summary,width`) // nolint: lll
381
382func (r *TableHTMLRenderer) renderTable(
383 w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
384 if entering {
385 _, _ = w.WriteString("<table")
386 if n.Attributes() != nil {
387 html.RenderAttributes(w, n, TableAttributeFilter)
388 }
389 _, _ = w.WriteString(">\n")
390 } else {
391 _, _ = w.WriteString("</table>\n")
392 }
393 return gast.WalkContinue, nil
394}
395
396// TableHeaderAttributeFilter defines attribute names which <thead> elements can have.
397//
398// - align: Deprecated since HTML4, Obsolete since HTML5
399// - bgcolor: Not Standardized
400// - char: Deprecated since HTML4, Obsolete since HTML5
401// - charoff: Deprecated since HTML4, Obsolete since HTML5
402// - valign: Deprecated since HTML4, Obsolete since HTML5.
403var TableHeaderAttributeFilter = html.GlobalAttributeFilter.ExtendString(`align,bgcolor,char,charoff,valign`)
404
405func (r *TableHTMLRenderer) renderTableHeader(
406 w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
407 if entering {
408 _, _ = w.WriteString("<thead")
409 if n.Attributes() != nil {
410 html.RenderAttributes(w, n, TableHeaderAttributeFilter)
411 }
412 _, _ = w.WriteString(">\n")
413 _, _ = w.WriteString("<tr>\n") // Header <tr> has no separate handle
414 } else {
415 _, _ = w.WriteString("</tr>\n")
416 _, _ = w.WriteString("</thead>\n")
417 if n.NextSibling() != nil {
418 _, _ = w.WriteString("<tbody>\n")
419 }
420 }
421 return gast.WalkContinue, nil
422}
423
424// TableRowAttributeFilter defines attribute names which <tr> elements can have.
425//
426// - align: Obsolete since HTML5
427// - bgcolor: Obsolete since HTML5
428// - char: Obsolete since HTML5
429// - charoff: Obsolete since HTML5
430// - valign: Obsolete since HTML5.
431var TableRowAttributeFilter = html.GlobalAttributeFilter.ExtendString(`align,bgcolor,char,charoff,valign`)
432
433func (r *TableHTMLRenderer) renderTableRow(
434 w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
435 if entering {
436 _, _ = w.WriteString("<tr")
437 if n.Attributes() != nil {
438 html.RenderAttributes(w, n, TableRowAttributeFilter)
439 }
440 _, _ = w.WriteString(">\n")
441 } else {
442 _, _ = w.WriteString("</tr>\n")
443 if n.Parent().LastChild() == n {
444 _, _ = w.WriteString("</tbody>\n")
445 }
446 }
447 return gast.WalkContinue, nil
448}
449
450// TableThCellAttributeFilter defines attribute names which table <th> cells can have.
451//
452// - abbr: [OK] Contains a short abbreviated description of the cell's content [NOT OK in <td>]
453// - align: Obsolete since HTML5
454// - axis: Obsolete since HTML5
455// - bgcolor: Not Standardized
456// - char: Obsolete since HTML5
457// - charoff: Obsolete since HTML5
458// - colspan: [OK] Number of columns that the cell is to span
459// - headers: [OK] This attribute contains a list of space-separated strings,
460// each corresponding to the id attribute of the <th> elements that apply to this element
461// - height: Deprecated since HTML4. Obsolete since HTML5
462// - rowspan: [OK] Number of rows that the cell is to span
463// - scope: [OK] This enumerated attribute defines the cells that the header
464// (defined in the <th>) element relates to [NOT OK in <td>]
465// - valign: Obsolete since HTML5
466// - width: Deprecated since HTML4. Obsolete since HTML5.
467var TableThCellAttributeFilter = html.GlobalAttributeFilter.ExtendString(`abbr,align,axis,bgcolor,char,charoff,colspan,headers,height,rowspan,scope,valign,width`) // nolint:lll
468
469// TableTdCellAttributeFilter defines attribute names which table <td> cells can have.
470//
471// - abbr: Obsolete since HTML5. [OK in <th>]
472// - align: Obsolete since HTML5
473// - axis: Obsolete since HTML5
474// - bgcolor: Not Standardized
475// - char: Obsolete since HTML5
476// - charoff: Obsolete since HTML5
477// - colspan: [OK] Number of columns that the cell is to span
478// - headers: [OK] This attribute contains a list of space-separated strings, each corresponding
479// to the id attribute of the <th> elements that apply to this element
480// - height: Deprecated since HTML4. Obsolete since HTML5
481// - rowspan: [OK] Number of rows that the cell is to span
482// - scope: Obsolete since HTML5. [OK in <th>]
483// - valign: Obsolete since HTML5
484// - width: Deprecated since HTML4. Obsolete since HTML5.
485var TableTdCellAttributeFilter = html.GlobalAttributeFilter.ExtendString(`abbr,align,axis,bgcolor,char,charoff,colspan,headers,height,rowspan,scope,valign,width`) // nolint: lll
486
487func (r *TableHTMLRenderer) renderTableCell(
488 w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
489 n := node.(*ast.TableCell)
490 tag := "td"
491 if n.Parent().Kind() == ast.KindTableHeader {
492 tag = "th"
493 }
494 if entering {
495 _, _ = fmt.Fprintf(w, "<%s", tag)
496 if n.Alignment != ast.AlignNone {
497 amethod := r.TableConfig.TableCellAlignMethod
498 if amethod == TableCellAlignDefault {
499 if r.Config.XHTML {
500 amethod = TableCellAlignAttribute
501 } else {
502 amethod = TableCellAlignStyle
503 }
504 }
505 switch amethod {
506 case TableCellAlignAttribute:
507 if _, ok := n.AttributeString("align"); !ok { // Skip align render if overridden
508 _, _ = fmt.Fprintf(w, ` align="%s"`, n.Alignment.String())
509 }
510 case TableCellAlignStyle:
511 v, ok := n.AttributeString("style")
512 var cob util.CopyOnWriteBuffer
513 if ok {
514 switch v := v.(type) {
515 case []byte:
516 cob = util.NewCopyOnWriteBuffer(v)
517 case string:
518 cob = util.NewCopyOnWriteBuffer([]byte(v))
519 }
520 cob.AppendByte(';')
521 }
522 style := fmt.Sprintf("text-align:%s", n.Alignment.String())
523 cob.AppendString(style)
524 n.SetAttributeString("style", cob.Bytes())
525 }
526 }
527 if n.Attributes() != nil {
528 if tag == "td" {
529 html.RenderAttributes(w, n, TableTdCellAttributeFilter) // <td>
530 } else {
531 html.RenderAttributes(w, n, TableThCellAttributeFilter) // <th>
532 }
533 }
534 _ = w.WriteByte('>')
535 } else {
536 _, _ = fmt.Fprintf(w, "</%s>\n", tag)
537 }
538 return gast.WalkContinue, nil
539}
540
541type table struct {
542 options []TableOption
543}
544
545// Table is an extension that allow you to use GFM tables .
546var Table = &table{
547 options: []TableOption{},
548}
549
550// NewTable returns a new extension with given options.
551func NewTable(opts ...TableOption) goldmark.Extender {
552 return &table{
553 options: opts,
554 }
555}
556
557func (e *table) Extend(m goldmark.Markdown) {
558 m.Parser().AddOptions(
559 parser.WithParagraphTransformers(
560 util.Prioritized(NewTableParagraphTransformer(), 200),
561 ),
562 parser.WithASTTransformers(
563 util.Prioritized(defaultTableASTTransformer, 0),
564 ),
565 )
566 m.Renderer().AddOptions(renderer.WithNodeRenderers(
567 util.Prioritized(NewTableHTMLRenderer(e.options...), 500),
568 ))
569}