|
2 | 2 |
|
3 | 3 | require 'html/pipeline/filter'
|
4 | 4 | require 'html/pipeline/text_filter'
|
5 |
| -require 'redcarpet' |
6 |
| -require 'rouge' |
7 |
| -require 'rouge/plugins/redcarpet' |
8 | 5 |
|
9 | 6 | module RedmineGitHosting
|
10 |
| - class HTMLwithRouge < Redcarpet::Render::HTML |
11 |
| - include Rouge::Plugins::Redcarpet |
12 |
| - end |
13 |
| - |
14 | 7 | class RedcarpetFilter < HTML::Pipeline::TextFilter
|
15 | 8 | def initialize(text, context = nil, result = nil)
|
16 | 9 | super text, context, result
|
17 | 10 | @text = @text.delete "\r"
|
18 | 11 | end
|
19 | 12 |
|
20 |
| - # Convert Markdown to HTML using the best available implementation |
21 |
| - # and convert into a DocumentFragment. |
| 13 | + # Convert Markdown to HTML using Redmine's WikiFormatting system |
| 14 | + # for consistency with Redmine's text formatting configuration. |
22 | 15 | #
|
23 | 16 | def call
|
24 |
| - html = self.class.renderer.render @text |
| 17 | + html = markdown_formatter.new(@text).to_html |
25 | 18 | html.rstrip!
|
26 | 19 | html
|
27 | 20 | end
|
28 | 21 |
|
29 |
| - def self.renderer |
30 |
| - @renderer ||= Redcarpet::Markdown.new HTMLwithRouge, markdown_options |
31 |
| - end |
| 22 | + private |
32 | 23 |
|
33 |
| - def self.markdown_options |
34 |
| - @markdown_options ||= { |
35 |
| - fenced_code_blocks: true, |
36 |
| - lax_spacing: true, |
37 |
| - strikethrough: true, |
38 |
| - autolink: true, |
39 |
| - tables: true, |
40 |
| - underline: true, |
41 |
| - highlight: true |
42 |
| - }.freeze |
| 24 | + def markdown_formatter |
| 25 | + # Find the markdown formatter from Redmine's WikiFormatting system |
| 26 | + formatter_name = Redmine::WikiFormatting.format_names.find { |name| name =~ /markdown/i } |
| 27 | + |
| 28 | + if formatter_name |
| 29 | + Redmine::WikiFormatting.formatter_for(formatter_name) |
| 30 | + else |
| 31 | + # Fallback to textile formatter if no markdown formatter is available |
| 32 | + Redmine::WikiFormatting.formatter_for('textile') |
| 33 | + end |
43 | 34 | end
|
44 |
| - |
45 |
| - private_class_method :markdown_options |
46 | 35 | end
|
47 | 36 | end
|
0 commit comments