Skip to content

Instantly share code, notes, and snippets.

@loveencounterflow
Last active August 29, 2015 14:08
Show Gist options
  • Save loveencounterflow/9d893beb37d12ad456fd to your computer and use it in GitHub Desktop.
Save loveencounterflow/9d893beb37d12ad456fd to your computer and use it in GitHub Desktop.
sample FROB extension / plugin for https://github.com/jonschlinkert/remarkable
############################################################################################################
### % [ title ] ( src ) ###
video_matcher = /^%\[([^\]]*)\]\s*\(([^)]+)\)/
#-----------------------------------------------------------------------------------------------------------
parse_video = ( state, silent ) ->
return false if state.src[ state.pos ] isnt '%'
match = video_matcher.exec state.src[ state[ 'pos' ] .. ]
return false unless match?
unless silent
description =
type: 'video'
title: match[ 1 ]
src: match[ 2 ]
level: state.level
state.push description
# every rule should set state.pos to a position after token's contents:
state.pos += match[ 0 ].length
return true
#-----------------------------------------------------------------------------------------------------------
render_video = ( tokens, idx ) -> # options
{ title, src, } = tokens[ idx ]
return "<video href='#{src}'>#{title}</video>"
#-----------------------------------------------------------------------------------------------------------
video_extension = ( self ) ->
self.inline.ruler.after 'backticks', 'video', parse_video
self.renderer.rules[ 'video' ] = render_video
return null
############################################################################################################
emphasis_chr = '='
#-----------------------------------------------------------------------------------------------------------
parse_emphasis = ( state, silent ) ->
return false if state.src[ state.pos ] isnt emphasis_chr
start = null
max = null
match_start = null
match_end = null
content = null
#.........................................................................................................
{ src, pos, posMax: pos_max, } = state
return false if ( chr = src[ pos ] ) isnt emphasis_chr
#.........................................................................................................
start = pos
pos += 1
pos += 1 while pos < pos_max and src[ pos ] isnt emphasis_chr
stop = pos
#.........................................................................................................
return false unless src[ pos ] is emphasis_chr
return false if stop is start + 1
unless silent
state.push
type: 'emphasis'
content: src[ start + 1 ... stop ]
block: false
level: state.level
#.........................................................................................................
state.pos = stop + 1
return true
#-----------------------------------------------------------------------------------------------------------
render_emphasis = ( tokens, idx ) -> # options
{ content, } = tokens[ idx ]
return "<em>#{content}</em>"
#-----------------------------------------------------------------------------------------------------------
emphasis_extension = ( self ) ->
self.inline.ruler.after 'backticks', 'emphasis', parse_emphasis
self.renderer.rules[ 'emphasis' ] = render_emphasis
return null
#-----------------------------------------------------------------------------------------------------------
remarkable_markdown_parser = ->
enable = 'full'
settings =
html: yes, # Enable HTML tags in source
xhtmlOut: no, # Use '/' to close single tags (<br />)
breaks: no, # Convert '\n' in paragraphs into <br>
langPrefix: 'language-', # CSS language prefix for fenced blocks
linkify: yes, # Autoconvert URL-like text to links
typographer: yes,
quotes: '“”‘’'
MD = new ( require 'remarkable' ) enable, settings
MD.use emphasis_extension
MD.use video_extension
source = """
This =is= 'awesome'(c): %[example movie](http://example.com)
"""
console.log html = MD.render source
@loveencounterflow
Copy link
Author

two very basic extensions for the remarkable MarkDown parser.

  • the emphasis_extension recognizes equals signs as markers for emphasis;
  • the video_extension recognizes %[title](href) markup and turns it into <video> tag (note: if you you want this to work in your own code, you must correct the rendering output, which is more imaginary than correct right now—this is a MarkDown syntax plugin example, not an HTML5 tutorial...).

the code also shows how to turn this MarkDown:

This =is= 'awesome'(c): %[example movie](http://example.com)

into this HTML:

<p>This <em>is</em> 'awesome’©: <video href='http://example.com'>example movie</video></p>

remarkable has been updated so these examples work out of the box; you probably still have to try and git-clone the dev branch until the distro on npmjs.org has been updated, too. in order to work, you must (as of 2014-11-08) patch https://github.com/jonschlinkert/remarkable/blob/dev/lib/renderer.js#L293 (or clone https://github.com/loveencounterflow/remarkable):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment