| 1 |
package extension |
| 2 |
|
| 3 |
import ( |
| 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 |
|
| 18 |
var escapedPipeCellListKey = parser.NewContextKey() |
| 19 |
|
| 20 |
type escapedPipeCell struct { |
| 21 |
Cell *ast.TableCell |
| 22 |
Pos []int |
| 23 |
Transformed bool |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
type TableCellAlignMethod int |
| 28 |
|
| 29 |
const ( |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
TableCellAlignDefault TableCellAlignMethod = iota |
| 34 |
|
| 35 |
|
| 36 |
TableCellAlignAttribute |
| 37 |
|
| 38 |
|
| 39 |
TableCellAlignStyle |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
TableCellAlignNone |
| 45 |
) |
| 46 |
|
| 47 |
|
| 48 |
type TableConfig struct { |
| 49 |
html.Config |
| 50 |
|
| 51 |
|
| 52 |
TableCellAlignMethod TableCellAlignMethod |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
type TableOption interface { |
| 57 |
renderer.Option |
| 58 |
|
| 59 |
SetTableOption(*TableConfig) |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func NewTableConfig() TableConfig { |
| 64 |
return TableConfig{ |
| 65 |
Config: html.NewConfig(), |
| 66 |
TableCellAlignMethod: TableCellAlignDefault, |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
func (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 |
|
| 80 |
type withTableHTMLOptions struct { |
| 81 |
value []html.Option |
| 82 |
} |
| 83 |
|
| 84 |
func (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 |
|
| 92 |
func (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 |
|
| 101 |
func WithTableHTMLOptions(opts ...html.Option) TableOption { |
| 102 |
return &withTableHTMLOptions{opts} |
| 103 |
} |
| 104 |
|
| 105 |
const optTableCellAlignMethod renderer.OptionName = "TableTableCellAlignMethod" |
| 106 |
|
| 107 |
type withTableCellAlignMethod struct { |
| 108 |
value TableCellAlignMethod |
| 109 |
} |
| 110 |
|
| 111 |
func (o *withTableCellAlignMethod) SetConfig(c *renderer.Config) { |
| 112 |
c.Options[optTableCellAlignMethod] = o.value |
| 113 |
} |
| 114 |
|
| 115 |
func (o *withTableCellAlignMethod) SetTableOption(c *TableConfig) { |
| 116 |
c.TableCellAlignMethod = o.value |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func WithTableCellAlignMethod(a TableCellAlignMethod) TableOption { |
| 121 |
return &withTableCellAlignMethod{a} |
| 122 |
} |
| 123 |
|
| 124 |
func 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 |
|
| 140 |
var tableDelimLeft = regexp.MustCompile(`^\s*\:\-+\s*$`) |
| 141 |
var tableDelimRight = regexp.MustCompile(`^\s*\-+\:\s*$`) |
| 142 |
var tableDelimCenter = regexp.MustCompile(`^\s*\:\-+\:\s*$`) |
| 143 |
var tableDelimNone = regexp.MustCompile(`^\s*\-+\s*$`) |
| 144 |
|
| 145 |
type tableParagraphTransformer struct { |
| 146 |
} |
| 147 |
|
| 148 |
var defaultTableParagraphTransformer = &tableParagraphTransformer{} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func NewTableParagraphTransformer() parser.ParagraphTransformer { |
| 153 |
return defaultTableParagraphTransformer |
| 154 |
} |
| 155 |
|
| 156 |
func (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 |
| 185 |
node.Lines().Set(i-2, last) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
func (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 |
|
| 258 |
func (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 |
|
| 289 |
type tableASTTransformer struct { |
| 290 |
} |
| 291 |
|
| 292 |
var defaultTableASTTransformer = &tableASTTransformer{} |
| 293 |
|
| 294 |
|
| 295 |
func NewTableASTTransformer() parser.ASTTransformer { |
| 296 |
return defaultTableASTTransformer |
| 297 |
} |
| 298 |
|
| 299 |
func (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 |
|
| 345 |
|
| 346 |
type TableHTMLRenderer struct { |
| 347 |
TableConfig |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
func 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 |
|
| 362 |
func (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 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
var TableAttributeFilter = html.GlobalAttributeFilter.ExtendString(`align,bgcolor,border,cellpadding,cellspacing,frame,rules,summary,width`) |
| 381 |
|
| 382 |
func (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 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
var TableHeaderAttributeFilter = html.GlobalAttributeFilter.ExtendString(`align,bgcolor,char,charoff,valign`) |
| 404 |
|
| 405 |
func (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") |
| 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 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
var TableRowAttributeFilter = html.GlobalAttributeFilter.ExtendString(`align,bgcolor,char,charoff,valign`) |
| 432 |
|
| 433 |
func (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 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
var TableThCellAttributeFilter = html.GlobalAttributeFilter.ExtendString(`abbr,align,axis,bgcolor,char,charoff,colspan,headers,height,rowspan,scope,valign,width`) |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
var TableTdCellAttributeFilter = html.GlobalAttributeFilter.ExtendString(`abbr,align,axis,bgcolor,char,charoff,colspan,headers,height,rowspan,scope,valign,width`) |
| 486 |
|
| 487 |
func (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 { |
| 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) |
| 530 |
} else { |
| 531 |
html.RenderAttributes(w, n, TableThCellAttributeFilter) |
| 532 |
} |
| 533 |
} |
| 534 |
_ = w.WriteByte('>') |
| 535 |
} else { |
| 536 |
_, _ = fmt.Fprintf(w, "</%s>\n", tag) |
| 537 |
} |
| 538 |
return gast.WalkContinue, nil |
| 539 |
} |
| 540 |
|
| 541 |
type table struct { |
| 542 |
options []TableOption |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
var Table = &table{ |
| 547 |
options: []TableOption{}, |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
func NewTable(opts ...TableOption) goldmark.Extender { |
| 552 |
return &table{ |
| 553 |
options: opts, |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
func (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 |
} |