Support Ruby 4 in the cmark version#430
Closed
davidcelis wants to merge 1 commit intogjtorikian:c-api-stablefrom
Closed
Support Ruby 4 in the cmark version#430davidcelis wants to merge 1 commit intogjtorikian:c-api-stablefrom
cmark version#430davidcelis wants to merge 1 commit intogjtorikian:c-api-stablefrom
Conversation
Owner
|
Hiya! The short answer is that I'm loathe to maintain the C-backed version. For this specific case, I can suggest one of two things:
class CustomRenderer < Commonmarker::HtmlRenderer
def link(node)
out('<a href="', escape_href(node.url), '"')
out(' title="', escape_html(node.title), '"') unless node.title.empty?
out(' rel="nofollow" target="_blank"') # Add custom attributes
with_node(node) { out(">", :children, "</a>") }
end
end
doc = Commonmarker.parse("[click here](https://example.com)")
renderer = CustomRenderer.new
html = renderer.render(doc)
# => "<p><a href=\"https://example.com\" rel=\"nofollow\" target=\"_blank\">click here</a></p>\n"I don't think it'll take very long to implement, since the code was available in the old library anyhow, and won't require me to write new Rust code.
require "html_pipeline"
require "commonmarker"
class LinkAttributeFilter < HTMLPipeline::NodeFilter
SELECTOR = Selma::Selector.new(match_element: "a")
def selector
SELECTOR
end
def handle_element(element)
element["rel"] = "nofollow"
element["target"] = "_blank"
end
end
pipeline = HTMLPipeline.new(
# yeah, this is incorrectly named, but it uses `commonmarker` underneath
convert_filter: HTMLPipeline::ConvertFilter::MarkdownFilter.new,
node_filters: [LinkAttributeFilter.new]
)
result = pipeline.call("[Example](https://example.com)")
puts result[:output]Would this work? |
Author
That is extremely fair 😂 Your suggestion to use |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I’m still using the
cmarkversion (which is hopefully still being maintained 😅), but that version’s gemspec remains locked to< 4.0. I believe we can just bump that to< 5.0and that the gem will work on Ruby 4 as is, though I’m still testing that out a bit. I’ve tried to update my application to the new comrak wrapper but I’ve been reliant on the ability to use a customCommonMarker::HTMLRenderersubclass that adds attributes to link nodes beyond the standardhrefandtitleattributes. Either custom renderers aren’t possible in the comrak version or I haven’t been able to figure out how to port this custom logic over based on available documentation (I’m happy to open a separate issue about this as well)