class BridgetownMdjs::Builder

Public Instance Methods

build() click to toggle source
# File lib/bridgetown-mdjs/builder.rb, line 5
def build
  # Set the kramdown input to use the extractions parser
  site.config.kramdown.input = "GFMExtractions"

  # Obtain method as a proc and call it from within either the Liquid or Ruby helper contexts
  method(:process_extractions).tap do |process_the|
    liquid_tag "mdjs_script" do |_attributes, tag|
      process_the.(tag.context.registers[:page][:markdown_extractions])
    end
    helper "mdjs_script", helpers_scope: true do
      process_the.(view.page.data.markdown_extractions)
    end
  end
end
process_extractions(extractions) click to toggle source
# File lib/bridgetown-mdjs/builder.rb, line 20
    def process_extractions(extractions)
      jscode = +""

      extractions&.each do |extraction|
        jscode << extraction.code if (extraction.lang == "js") && (extraction.meta == "script")
      end

      if jscode.present?
        return <<~HTML.html_safe
          <script type="module">
          #{jscode}</script>
        HTML
      end

      jscode
    end