Class: Glancer::Utils::MarkdownHelper
- Inherits:
-
Object
- Object
- Glancer::Utils::MarkdownHelper
- Defined in:
- lib/glancer/utils/markdown_helper.rb
Class Method Summary collapse
- .extract_sql_from_markdown(markdown) ⇒ Object
-
.highlight_mentions(text, schema_base: nil, valid_tables: nil) ⇒ Object
Wraps @word tokens with a highlight span.
- .markdown_to_html(markdown_text, schema_base: nil, valid_tables: nil) ⇒ Object
Class Method Details
.extract_sql_from_markdown(markdown) ⇒ Object
50 51 52 53 |
# File 'lib/glancer/utils/markdown_helper.rb', line 50 def self.extract_sql_from_markdown(markdown) match = markdown.match(/```sql\n(.+?)\n```/m) match ? match[1].strip : "" end |
.highlight_mentions(text, schema_base: nil, valid_tables: nil) ⇒ Object
Wraps @word tokens with a highlight span. Applied before markdown so that the renderer preserves the inline HTML. Skips content inside backtick spans and fenced code blocks so code examples are not affected.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/glancer/utils/markdown_helper.rb', line 25 def self.highlight_mentions(text, schema_base: nil, valid_tables: nil) valid_set = valid_tables&.to_set # Split on fenced code blocks and inline backtick spans to skip them. parts = text.split(/(```[\s\S]*?```|`[^`]*`)/) parts.map.with_index do |part, idx| if idx.odd? part else # Negative lookbehind prevents matching @ inside emails or identifiers. part.gsub(/(?<![a-zA-Z0-9._])@([a-zA-Z]\w*)/) do table = Regexp.last_match(1) next "@#{table}" if valid_set && !valid_set.include?(table) if schema_base href = "#{schema_base}?table=#{table}" attrs = %( href="#{href}" target="_blank" rel="noopener noreferrer" tabindex="0") %(<a class="glancer-mention" data-table="#{table}"#{attrs}>@#{table}</a>) else %(<a class="glancer-mention" data-table="#{table}" href="#" tabindex="0">@#{table}</a>) end end end end.join end |
.markdown_to_html(markdown_text, schema_base: nil, valid_tables: nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/glancer/utils/markdown_helper.rb', line 7 def self.markdown_to_html(markdown_text, schema_base: nil, valid_tables: nil) content = Commonmarker.to_html(highlight_mentions(markdown_text, schema_base: schema_base, valid_tables: valid_tables), options: { parse: { smart: true }, render: { unsafe: true, github_pre_lang: true } }, plugins: { syntax_highlighter: { theme: "InspiredGitHub" } }) content.gsub!(%r{<table.*?</table>}m) do |table_html| %(<div class="table-scroll-wrapper"><div class="table-scroll-inner">#{table_html}</div></div>) end content end |