Welcome to mirror list, hosted at ThFree Co, Russian Federation.

common_mark.rb « markdown_engines « filter « banzai « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf368e28beb696330bc172cad9c1cbf769d474d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
# frozen_string_literal: true

# `CommonMark` markdown engine for GitLab's Banzai markdown filter.
# This module is used in Banzai::Filter::MarkdownFilter.
# Used gem is `commonmarker` which is a ruby wrapper for libcmark (CommonMark parser)
# including GitHub's GFM extensions.
# We now utilize the renderer built in `C`, rather than the ruby based renderer.
# Homepage: https://github.com/gjtorikian/commonmarker
module Banzai
  module Filter
    module MarkdownEngines
      class CommonMark
        EXTENSIONS = [
          :autolink,      # provides support for automatically converting URLs to anchor tags.
          :strikethrough, # provides support for strikethroughs.
          :table          # provides support for tables.
        ].freeze

        PARSE_OPTIONS = [
          :FOOTNOTES,                  # parse footnotes.
          :STRIKETHROUGH_DOUBLE_TILDE, # parse strikethroughs by double tildes (as redcarpet does).
          :VALIDATE_UTF8               # replace illegal sequences with the replacement character U+FFFD.
        ].freeze

        RENDER_OPTIONS = [
          :GITHUB_PRE_LANG,  # use GitHub-style <pre lang> for fenced code blocks.
          :FOOTNOTES,        # render footnotes.
          :FULL_INFO_STRING, # include full info strings of code blocks in separate attribute.
          :UNSAFE            # allow raw/custom HTML and unsafe links.
        ].freeze

        def initialize(context)
          @context = context
        end

        def render(text)
          CommonMarker.render_html(text, render_options, EXTENSIONS)
        end

        private

        def render_options
          @context[:no_sourcepos] ? render_options_no_sourcepos : render_options_sourcepos
        end

        def render_options_no_sourcepos
          RENDER_OPTIONS
        end

        def render_options_sourcepos
          render_options_no_sourcepos + [
            :SOURCEPOS # enable embedding of source position information
          ].freeze
        end
      end
    end
  end
end