Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional <thead>, <tr>, <th>, <td> attributes render #78

Merged
merged 1 commit into from
Dec 26, 2019

Conversation

zzwx
Copy link
Contributor

@zzwx zzwx commented Dec 25, 2019

Bumped into a couple of issues with table headers rendering and it required adding these attributes render.

In particular, align used in <tr> misbehaved in my use case while working fine in <td>.

So I have to pass class attribute instead when necessary.

Also align is marked as deprecated, although I don't think it was the reason for my issue.

@zzwx zzwx changed the title Additional <thead>, <tr>, <tr>, <td> attributes render Additional <thead>, <tr>, <th>, <td> attributes render Dec 25, 2019
@zzwx
Copy link
Contributor Author

zzwx commented Dec 25, 2019

@yuin This is right. This allows a user to override "align" attribute. But if they don't, "align" is rendered as before.

@yuin yuin merged commit 2c9db0c into yuin:master Dec 26, 2019
@djibe
Copy link

djibe commented Apr 29, 2020

Hi @zzwx
How can we override "align" attribute and will Goldmark stop generating wrong td align="right" ?
Thx for your help

@zzwx
Copy link
Contributor Author

zzwx commented May 1, 2020

@djibe, @yuin

I actually don't see that it's possible to remove this attribute easily, or block from final rendering.

I add necessary to me text-* classes manually using ASTTransformer with this method:

func (c *globalMarkdownClassTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
  ast.Walk(node, func(n ast.Node, entering bool) (status ast.WalkStatus, err error) {
  if tableCell, ok := n.(*extAst.TableCell); ok {
    if entering {
				//tableCell.SetAttributeString("align", []byte(""))
				if tableCell.Parent().Kind() == extAst.KindTableHeader || tableCell.Parent().Kind() == extAst.KindTableRow {
					switch tableCell.Alignment {
					case extAst.AlignRight:
						tableCell.SetAttributeString("class", []byte("text-right"))
					case extAst.AlignCenter:
						tableCell.SetAttributeString("class", []byte("text-center"))
					case extAst.AlignLeft:
						tableCell.SetAttributeString("class", []byte("text-left"))
					}
				}
    }
  }
  return ast.WalkContinue, nil
  })
}

The tableCell.SetAttributeString("align", []byte("")) only makes the attribute rendered as align, still rendered, just without a value. Setting to nil causes panic, I wish nil had a special meaning of blocking the attribute from rendering, because apparently it adds to the list of attributes somewhere later on. Or at least there was another way.

Any thoughts, @yuin?

@djibe
Copy link

djibe commented May 1, 2020

Thanks a lot for your answer.

@yuin
Copy link
Owner

yuin commented May 2, 2020

You can remove align attributes by replacing ast.TableCell.Align with ast.AlignNone .

Currently, goldmark renders ast.TableCell.Align as td#align attribute bacause Official GFM spec defines it.

But it is sound better for me that built-in HTML renderer renders ast.TableCell.Align as style if user do not set html.WithXHTML() option.

@zzwx
Copy link
Contributor Author

zzwx commented May 3, 2020

This is awesome @yuin, thank you! So basically this one of the ways to substitute align with class:

func (c *globalMarkdownClassTransformer) Transform(node *ast.Document, reader text.Reader, pc parser.Context) {
  ast.Walk(node, func(n ast.Node, entering bool) (status ast.WalkStatus, err error) {
  if tableCell, ok := n.(*extAst.TableCell); ok {
    if entering {
		if tableCell.Parent().Kind() == extAst.KindTableHeader || tableCell.Parent().Kind() == extAst.KindTableRow {
				switch tableCell.Alignment {
				case extAst.AlignRight:
					tableCell.SetAttributeString("class", []byte("text-right"))
				case extAst.AlignCenter:
					tableCell.SetAttributeString("class", []byte("text-center"))
				case extAst.AlignLeft:
					tableCell.SetAttributeString("class", []byte("text-left"))
				}
				//tableCell.SetAttributeString("align", []byte("")) // Won't work, still renders the "align", just without value
				tableCell.Alignment = extAst.AlignNone // Make renderer forget about default "align" attribute
		}
    }
  }
  return ast.WalkContinue, nil
  })
}

yuin added a commit that referenced this pull request Jul 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants