Module: MarkdownServer::Helpers::MarkdownHelpers
- Defined in:
- lib/markdown_server/helpers/markdown_helpers.rb
Instance Method Summary collapse
- #extract_toc(html) ⇒ Object
- #parse_frontmatter(content) ⇒ Object
- #render_frontmatter_value(value) ⇒ Object
- #render_inline_wiki_links(str) ⇒ Object
- #render_markdown(text) ⇒ Object
- #resolve_wiki_link(name) ⇒ Object
- #syntax_highlight(code, language) ⇒ Object
Instance Method Details
#extract_toc(html) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 206 def extract_toc(html) headings = [] html.scan(/<h([1-6])\s[^>]*id="([^"]*)"[^>]*>(.*?)<\/h\1>/mi) do |level, id, text| clean_text = text.gsub(/<sup[^>]*id="fnref:[^"]*"[^>]*>.*?<\/sup>/i, "").gsub(/<[^>]+>/, "").strip headings << { level: level.to_i, id: id, text: clean_text } end headings end |
#parse_frontmatter(content) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 4 def parse_frontmatter(content) if content =~ /\A---\s*\n(.*?\n)---\s*\n(.*)/m begin = YAML.safe_load($1, permitted_classes: [Date, Time]) body = $2 [, body] rescue => e [nil, content] end else [nil, content] end end |
#render_frontmatter_value(value) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 170 def render_frontmatter_value(value) case value when Array value.map { |v| str = v.to_s if str.include?("[[") render_inline_wiki_links(str) else %(<span class="tag">#{h(str)}</span>) end }.join(" ") when String if value =~ /\Ahttps?:\/\// %(<a href="#{h(value)}" target="_blank" rel="noopener">#{h(value)}</a>) elsif value.include?("[[") render_inline_wiki_links(value) elsif value.length > 120 render_markdown(value) else h(value) end when Numeric, TrueClass, FalseClass h(value.to_s) when NilClass %(<span class="empty">—</span>) else h(value.to_s) end end |
#render_inline_wiki_links(str) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 141 def render_inline_wiki_links(str) result = "" last_end = 0 str.scan(/\[\[([^\]]+)\]\]/) do |match| raw = match[0] m_start = $~.begin(0) m_end = $~.end(0) result += h(str[last_end...m_start]) target, display = raw.include?("|") ? raw.split("|", 2) : [raw, nil] label = display || target if target.start_with?("#") anchor = target[1..].downcase.gsub(/\s+/, "-").gsub(/[^\w-]/, "") result += %(<a class="wiki-link" href="##{h(anchor)}">#{h(label)}</a>) else file_part, anchor_part = target.split("#", 2) anchor_suffix = anchor_part ? "##{anchor_part.downcase.gsub(/\s+/, '-').gsub(/[^\w-]/, '')}" : "" resolved = resolve_wiki_link(file_part) if resolved result += %(<a class="wiki-link" href="/browse/#{encode_path_component(resolved).gsub('%2F', '/')}#{anchor_suffix}">#{h(label)}</a>) else result += %(<span class="wiki-link broken">#{h(label)}</span>) end end last_end = m_end end result += h(str[last_end..]) result end |
#render_markdown(text) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 18 def render_markdown(text) # Convert mermaid code fences to raw HTML divs before Kramdown so Rouge # never touches them and the content is preserved exactly for Mermaid.js. text = text.gsub(/^```mermaid[ \t]*\r?\n([\s\S]*?)^```[ \t]*(\r?\n|\z)/m) do "<div class=\"mermaid\">\n#{h($1.rstrip)}\n</div>\n\n" end # Run plugin markdown transformations (e.g. Bible citation auto-linking) settings.plugins.each { |p| text = p.transform_markdown(text) } # Process wiki links BEFORE Kramdown so that | isn't consumed as # a GFM table delimiter. text = text.gsub(/\[\[([^\]]+)\]\]/) do raw = $1 if raw.include?("|") target, display = raw.split("|", 2) else target = raw display = nil end if target.start_with?("#") heading_text = target[1..] anchor = heading_text.downcase.gsub(/\s+/, "-").gsub(/[^\w-]/, "") label = display || heading_text %(<a class="wiki-link" href="##{h(anchor)}">#{h(label)}</a>) else file_part, anchor_part = target.split("#", 2) anchor_suffix = anchor_part ? "##{anchor_part.downcase.gsub(/\s+/, '-').gsub(/[^\w-]/, '')}" : "" resolved = resolve_wiki_link(file_part) label = display || target if resolved %(<a class="wiki-link" href="/browse/#{encode_path_component(resolved).gsub('%2F', '/')}#{anchor_suffix}">#{h(label)}</a>) else %(<span class="wiki-link broken">#{h(label)}</span>) end end end html = Kramdown::Document.new( text, input: "GFM", syntax_highlighter: "rouge", syntax_highlighter_opts: { default_lang: "text" }, hard_wrap: settings.hard_wrap ).to_html # Restore non-numeric footnote labels: Kramdown converts all footnote # references to sequential numbers, but we want [^abc] to display "abc". html = html.gsub(%r{<sup id="fnref:([^"]+)"[^>]*>\s*<a href="#fn:\1"[^>]*>\d+</a>\s*</sup>}m) do full_match = $& name = $1 if name =~ /\A\d+\z/ full_match else %(<sup id="fnref:#{name}" role="doc-noteref"><a href="#fn:#{name}" class="footnote" rel="footnote">#{h(name)}</a></sup>) end end html.gsub(%r{<li id="fn:([^"]+)"[^>]*>\s*<p>}m) do full_match = $& name = $1 if name =~ /\A\d+\z/ full_match else %(<li id="fn:#{name}"><p><strong>#{h(name)}:</strong> ) end end end |
#resolve_wiki_link(name) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 87 def resolve_wiki_link(name) filename = "#{name}.md" base = File.realpath(root_dir) # On case-sensitive filesystems (Linux/Docker), FNM_CASEFOLD doesn't help # with directory listings. Try exact filename first, then lowercased variant. candidates = [filename] candidates << filename.downcase if filename != filename.downcase # Check the current file's directory first (exact case, then case-insensitive) if @current_wiki_dir local_exact = nil local_ci = nil candidates.each do |fn| Dir.glob(File.join(@current_wiki_dir, fn)).each do |path| real = File.realpath(path) rescue next next unless real.start_with?(base) relative = real.sub("#{base}/", "") first_segment = relative.split("/").first next if EXCLUDED.include?(first_segment) || first_segment&.start_with?(".") if File.basename(real) == filename local_exact = relative break else local_ci ||= relative end end break if local_exact end return local_exact if local_exact return local_ci if local_ci end # Fall back to global recursive search exact_match = nil ci_match = nil candidates.each do |fn| Dir.glob(File.join(base, "**", fn)).each do |path| real = File.realpath(path) rescue next next unless real.start_with?(base) relative = real.sub("#{base}/", "") first_segment = relative.split("/").first next if EXCLUDED.include?(first_segment) || first_segment&.start_with?(".") if File.basename(real) == filename exact_match ||= relative else ci_match ||= relative end end break if exact_match end exact_match || ci_match end |
#syntax_highlight(code, language) ⇒ Object
200 201 202 203 204 |
# File 'lib/markdown_server/helpers/markdown_helpers.rb', line 200 def syntax_highlight(code, language) formatter = Rouge::Formatters::HTML.new lexer = Rouge::Lexer.find_fancy(language) || Rouge::Lexers::PlainText.new formatter.format(lexer.lex(code)) end |