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

Don't warn on top-level comments #104

Merged
merged 1 commit into from
Sep 28, 2023
Merged

Conversation

mtrmac
Copy link
Contributor

@mtrmac mtrmac commented Sep 25, 2023

Motivated by containers/image#2128 .

Run the tests with -v to see that there are no warnings.

Add comment test cases

Signed-off-by: Miloslav Trmač <[email protected]>
@thaJeztah
Copy link
Collaborator

Run the tests with -v to see that there are no warnings.

We should probably have an option to set the output of those logs (both to allow consumers to decide if they want logging, but it would also allow tests to verify the behavior).

@thaJeztah
Copy link
Collaborator

Interestingly, it looks like blackfriday already has code to handle HTML comments, but doesn't assign a dedicated type to it;

// HTML comment, lax form
func (p *Markdown) htmlComment(data []byte, doRender bool) int {
i := p.inlineHTMLComment(data)
// needs to end with a blank line
if j := p.isEmpty(data[i:]); j > 0 {
size := i + j
if doRender {
// trim trailing newlines
end := size
for end > 0 && data[end-1] == '\n' {
end--
}
block := p.addBlock(HTMLBlock, data[:end])
finalizeHTMLBlock(block)
}
return size
}
return 0
}

Mostly out of curiosity, I patched the vendor directory to see if it would work to assign a dedicated type for that, and it looks like that works, so perhaps I'll open a PR for that in blackfriday;

diff --git a/md2man/roff.go b/md2man/roff.go
index c238a78..cc786db 100644
--- a/md2man/roff.go
+++ b/md2man/roff.go
@@ -165,13 +165,8 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
                return blackfriday.GoToNext
        case blackfriday.TableCell:
                r.handleTableCell(w, node, entering)
-       case blackfriday.HTMLSpan:
-               // ignore other HTML tags
-       case blackfriday.HTMLBlock:
-               if bytes.HasPrefix(node.Literal, []byte("<!--")) {
-                       break // ignore comments, no warning
-               }
-               fmt.Fprintln(os.Stderr, "WARNING: go-md2man does not handle node type "+node.Type.String())
+       case blackfriday.HTMLSpan, blackfriday.HTMLComment:
+               // ignore other HTML tags and comments
        default:
                fmt.Fprintln(os.Stderr, "WARNING: go-md2man does not handle node type "+node.Type.String())
        }
diff --git a/vendor/github.com/russross/blackfriday/v2/block.go b/vendor/github.com/russross/blackfriday/v2/block.go
index dcd61e6..59d6235 100644
--- a/vendor/github.com/russross/blackfriday/v2/block.go
+++ b/vendor/github.com/russross/blackfriday/v2/block.go
@@ -430,7 +430,7 @@ func (p *Markdown) htmlComment(data []byte, doRender bool) int {
                        for end > 0 && data[end-1] == '\n' {
                                end--
                        }
-                       block := p.addBlock(HTMLBlock, data[:end])
+                       block := p.addBlock(HTMLComment, data[:end])
                        finalizeHTMLBlock(block)
                }
                return size
diff --git a/vendor/github.com/russross/blackfriday/v2/node.go b/vendor/github.com/russross/blackfriday/v2/node.go
index 04e6050..299f54e 100644
--- a/vendor/github.com/russross/blackfriday/v2/node.go
+++ b/vendor/github.com/russross/blackfriday/v2/node.go
@@ -26,6 +26,7 @@ const (
        Image
        Text
        HTMLBlock
+       HTMLComment
        CodeBlock
        Softbreak
        Hardbreak
@@ -53,6 +54,7 @@ var nodeTypeNames = []string{
        Image:          "Image",
        Text:           "Text",
        HTMLBlock:      "HTMLBlock",
+       HTMLComment:    "HTMLComment",
        CodeBlock:      "CodeBlock",
        Softbreak:      "Softbreak",
        Hardbreak:      "Hardbreak",

Copy link
Collaborator

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@thaJeztah
Copy link
Collaborator

@cpuguy83 PTAL

@thaJeztah
Copy link
Collaborator

@cpuguy83 cpuguy83 merged commit e8098c3 into cpuguy83:master Sep 28, 2023
6 checks passed
@mtrmac mtrmac deleted the comments branch October 2, 2023 16:01
@cpuguy83
Copy link
Owner

I published v2.0.3 with this patch (and a few others).

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