Skip to content

Feature request: support default processors in configuration #123

@ianyamey

Description

@ianyamey

There's currently no way to configure default HTML processors globally. If you want a processor to run on every markdownify call, you have to either:

  1. Pass process: [MyProcessor] to every markdownify call in every view
  2. Prepend Perron::MarkdownHelper to inject defaults — which works, but feels like it should be a first-class config option

For example, I have a processor that fixes an accessibility issue with CommonMarker's anchor links (adding tabindex="-1" to aria-hidden="true" anchors). This needs to run on all rendered markdown, so I'm currently doing:

Rails.application.config.to_prepare do
  Perron::MarkdownHelper.prepend(Module.new do
    def markdownify(content = nil, process: [], &block)
      super(content, process: process + [AnchorTabindexProcessor], &block)
    end
  end)
end

Proposal

Add a default_processors configuration option:

Perron.configure do |config|
  config.default_processors = [AnchorTabindexProcessor, "target_blank"]
end

These would be prepended to whatever is passed via process: in individual markdownify calls, so per-call processors still work as expected:

<%# Runs AnchorTabindexProcessor, target_blank (from defaults), plus lazy_load_images %>
<%= markdownify @resource.content, process: %w[lazy_load_images] %>

Implementation

This could be as simple as:

  1. Adding default_processors to Perron::Configuration (defaulting to [])
  2. Merging them in MarkdownHelper#markdownify:
def markdownify(content = nil, process: [], &block)
  text = block_given? ? capture(&block).strip_heredoc : content
  all_processors = Perron.configuration.default_processors + Array(process)
  Perron::Markdown.render(text, processors: all_processors)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions