Giter Site home page Giter Site logo

raghubetina / kramdown-parser-gfm-extractions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bridgetownrb/kramdown-parser-gfm-extractions

0.0 1.0 0.0 7 KB

A subclass of Kramdown's GFM parser which extracts fenced code blocks featuring meta (aka `js script`)

License: MIT License

Ruby 100.00%

kramdown-parser-gfm-extractions's Introduction

kramdown-parser-gfm-extractions

A Kramdown parser extension which provides support for extracting fenced code blocks featuring meta (for example js script or html preview-story). Useful for supporting the Markdown JavaScript (mdjs) format.

Installation

Run this command to add the gem to your project's Gemfile.

bundle add kramdown-parser-gfm-extractions

Usage

Simply require the gem, pass the relevant input option to Kramdown, and you'll get the appropriate HTML output as well as an array of extractions.

require "kramdown"
require "kramdown-parser-gfm-extractions"

text = <<~MD
  Hello **folks**.

  ```js script
  import "Foo" from "./bar.js"
  ```

  ```ruby
  a = 1 + 2
  ```

  ```html preview-story
  <p>I'm a preview!</p>
  ```

  This is _Markdown!_
MD

doc = Kramdown::Document.new(text, {input: :GFMExtractions})
html = doc.to_html
extractions = doc.root.options[:extractions]

In this example, the js script block and the html preview-story block would both be extracted. In the list of extractions available via doc.root.options[:extractions], you'd obtain hashes with the following keys:

  • lang - the language code (js, html, etc.)
  • meta - the meta string (script, preview-story, etc.)
  • code - the actual code block verbatim

By default, the extracted code is still output to the HTML but contained within a custom tag called kramdown-extraction containing an inert template tag with the rendered output of the syntax processor. If you wish to customize or turn off this behavior, pass these options along to Kramdown::Document:

  • include_extraction_tags - set to false to entirely remove the extraction tags
  • include_code_in_extractions - set to false to strip the rendered code templates out of the extraction tags (but still keep the tags themselves)

Testing

  • Run bundle exec rake test to run the test suite

Contributing

  1. Fork it (https://github.com/bridgetownrb/kramdown-parser-gfm-extractions/fork)
  2. Clone the fork using git clone to your local development machine.
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

kramdown-parser-gfm-extractions's People

Contributors

jaredcwhite avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.