Class: MarkdownIt::Parser
- Inherits:
-
Object
- Object
- MarkdownIt::Parser
- Includes:
- Common::Utils
- Defined in:
- lib/motion-markdown-it/index.rb
Constant Summary
Constants included from Common::Utils
Common::Utils::DIGITAL_ENTITY_TEST_RE, Common::Utils::ENTITY_RE, Common::Utils::HTML_ESCAPE_REPLACE_RE, Common::Utils::HTML_ESCAPE_TEST_RE, Common::Utils::HTML_REPLACEMENTS, Common::Utils::REGEXP_ESCAPE_RE, Common::Utils::UNESCAPE_ALL_RE, Common::Utils::UNESCAPE_MD_RE, Common::Utils::UNICODE_PUNCT_RE
Instance Attribute Summary collapse
-
#block ⇒ Object
Returns the value of attribute block.
-
#core ⇒ Object
Returns the value of attribute core.
-
#inline ⇒ Object
Returns the value of attribute inline.
-
#linkify ⇒ Object
Returns the value of attribute linkify.
-
#normalizeLink ⇒ Object
Returns the value of attribute normalizeLink.
-
#normalizeLinkText ⇒ Object
Returns the value of attribute normalizeLinkText.
-
#options ⇒ Object
Returns the value of attribute options.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
-
#validateLink ⇒ Object
Returns the value of attribute validateLink.
Instance Method Summary collapse
-
#configure(presets) ⇒ Object
chainable, internal MarkdownIt.configure(presets).
-
#disable(list, ignoreInvalid) ⇒ Object
chainable MarkdownIt.disable(list, ignoreInvalid) - list (String|Array): rule name or list of rule names to disable.
-
#enable(list, ignoreInvalid) ⇒ Object
chainable MarkdownIt.enable(list, ignoreInvalid) - list (String|Array): rule name or list of rule names to enable - ignoreInvalid (Boolean): set ‘true` to ignore errors when rule not found.
-
#initialize(presetName = :default, options = {}) ⇒ Parser
constructor
new MarkdownIt([presetName, options]) - presetName (String): optional, ‘commonmark` / `zero` - options (Object).
-
#parse(src, env) ⇒ Object
internal MarkdownIt.parse(src, env) -> Array - src (String): source string - env (Object): environment sandbox.
-
#parseInline(src, env) ⇒ Object
internal MarkdownIt.parseInline(src, env) -> Array - src (String): source string - env (Object): environment sandbox.
-
#render(src, env = {}) ⇒ Object
MarkdownIt.render(src [, env]) -> String - src (String): source string - env (Object): environment sandbox.
-
#renderInline(src, env = {}) ⇒ Object
MarkdownIt.renderInline(src [, env]) -> String - src (String): source string - env (Object): environment sandbox.
-
#set(options) ⇒ Object
chainable MarkdownIt.set(options).
-
#to_html(src, env = {}) ⇒ Object
——————————————————————————.
-
#use(plugin, *args) ⇒ Object
chainable MarkdownIt.use(plugin, params).
Methods included from Common::Utils
#arrayReplaceAt, #assign, #escapeHtml, #escapeRE, #fromCodePoint, #isMdAsciiPunct, #isPunctChar, #isValidEntityCode, #isWhiteSpace, #normalizeReference, #replaceEntityPattern, #unescapeAll, #unescapeMd
Constructor Details
#initialize(presetName = :default, options = {}) ⇒ Parser
new MarkdownIt([presetName, options])
-
presetName (String): optional, ‘commonmark` / `zero`
-
options (Object)
Creates parser instanse with given config. Can be called without ‘new`.
##### presetName
MarkdownIt provides named presets as a convenience to quickly enable/disable active syntax rules and options for common use cases.
-
[“commonmark”](github.com/markdown-it/markdown-it/blob/master/lib/presets/commonmark.js) - configures parser to strict [CommonMark](commonmark.org/) mode.
-
[default](github.com/markdown-it/markdown-it/blob/master/lib/presets/default.js) - similar to GFM, used when no preset name given. Enables all available rules, but still without html, typographer & autolinker.
-
[“zero”](github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js) - all rules disabled. Useful to quickly setup your config via ‘.enable()`. For example, when you need only `bold` and `italic` markup and nothing else.
##### options:
-
__html__ - ‘false`. Set `true` to enable HTML tags in source. Be careful! That’s not safe! You may need external sanitizer to protect output from XSS. It’s better to extend features via plugins, instead of enabling HTML.
-
__xhtmlOut__ - ‘false`. Set `true` to add ’/‘ when closing single tags (`<br />`). This is needed only for full CommonMark compatibility. In real world you will need HTML output.
-
__breaks__ - ‘false`. Set `true` to convert `n` in paragraphs into `
`. -
__langPrefix__ - ‘language-`. CSS language class prefix for fenced blocks. Can be useful for external highlighters.
-
__linkify__ - ‘false`. Set `true` to autoconvert URL-like text to links.
-
__typographer__ - ‘false`. Set `true` to enable [some language-neutral replacement](github.com/markdown-it/markdown-it/blob/master/lib/rules_core/replacements.js) + quotes beautification (smartquotes).
-
__quotes__ - ‘“”‘’`, string. Double + single quotes replacement pairs, when typographer enabled and smartquotes on. Set doubles to ’«»‘ for Russian, ’„“‘ for German.
-
__highlight__ - ‘null`. Highlighter function for fenced code blocks. Highlighter `function (str, lang)` should return escaped HTML. It can also return empty string if the source was not changed and should be escaped externaly.
##### Example
“‘javascript // commonmark mode var md = require(’markdown-it’)(‘commonmark’);
// default mode var md = require(‘markdown-it’)();
// enable everything var md = require(‘markdown-it’)(
html: true,
linkify: true,
typographer: true
); “‘
##### Syntax highlighting
“‘js var hljs = require(’highlight.js’) // highlightjs.org/
var md = require(‘markdown-it’)({
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(lang, str).value;
} catch (__) {}
}
try {
return hljs.highlightAuto(str).value;
} catch (__) {}
return ''; // use external default escaping
}
}); “‘
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/motion-markdown-it/index.rb', line 193 def initialize(presetName = :default, = {}) if .empty? if presetName.is_a? Hash = presetName presetName = :default end end # MarkdownIt#inline -> ParserInline # # Instance of [[ParserInline]]. You may need it to add new rules when # writing plugins. For simple rules control use [[MarkdownIt.disable]] and # [[MarkdownIt.enable]]. @inline = ParserInline.new # MarkdownIt#block -> ParserBlock # # Instance of [[ParserBlock]]. You may need it to add new rules when # writing plugins. For simple rules control use [[MarkdownIt.disable]] and # [[MarkdownIt.enable]]. @block = ParserBlock.new # MarkdownIt#core -> Core # # Instance of [[Core]] chain executor. You may need it to add new rules when # writing plugins. For simple rules control use [[MarkdownIt.disable]] and # [[MarkdownIt.enable]]. @core = ParserCore.new # MarkdownIt#renderer -> Renderer # # Instance of [[Renderer]]. Use it to modify output look. Or to add rendering # rules for new token types, generated by plugins. # # ##### Example # # ```javascript # var md = require('markdown-it')(); # # function myToken(tokens, idx, options, env, self) { # //... # return result; # }; # # md.renderer.rules['my_token'] = myToken # ``` # # See [[Renderer]] docs and [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js). @renderer = Renderer.new # MarkdownIt#linkify -> LinkifyIt # # [linkify-it](https://github.com/markdown-it/linkify-it) instance. # Used by [linkify](https://github.com/markdown-it/markdown-it/blob/master/lib/rules_core/linkify.js) # rule. @linkify = ::Linkify.new # MarkdownIt#validateLink(url) -> Boolean # # Link validation function. CommonMark allows too much in links. By default # we disable `javascript:` and `vbscript:` schemas. You can change this # behaviour. # # ```javascript # var md = require('markdown-it')(); # // enable everything # md.validateLink = function () { return true; } # ``` @validateLink = VALIDATE_LINK # MarkdownIt#normalizeLink(url) -> String # # Function used to encode link url to a machine-readable format, # which includes url-encoding, punycode, etc. @normalizeLink = NORMALIZE_LINK # MarkdownIt#normalizeLinkText(url) -> String # # Function used to decode link url to a human-readable format` @normalizeLinkText = NORMALIZE_LINK_TEXT # Expose utils & helpers for easy acces from plugins # TODO I don't know if these (utils and helpers) are really needed # MarkdownIt#utils -> utils # # Assorted utility functions, useful to write plugins. See details # [here](https://github.com/markdown-it/markdown-it/blob/master/lib/common/utils.js). # this.utils = utils; # MarkdownIt#helpers -> helpers # # Link components parser functions, useful to write plugins. See details # [here](https://github.com/markdown-it/markdown-it/blob/master/lib/helpers). # this.helpers = helpers; @options = {} configure(presetName) set() if end |
Instance Attribute Details
#block ⇒ Object
Returns the value of attribute block.
103 104 105 |
# File 'lib/motion-markdown-it/index.rb', line 103 def block @block end |
#core ⇒ Object
Returns the value of attribute core.
104 105 106 |
# File 'lib/motion-markdown-it/index.rb', line 104 def core @core end |
#inline ⇒ Object
Returns the value of attribute inline.
102 103 104 |
# File 'lib/motion-markdown-it/index.rb', line 102 def inline @inline end |
#linkify ⇒ Object
Returns the value of attribute linkify.
110 111 112 |
# File 'lib/motion-markdown-it/index.rb', line 110 def linkify @linkify end |
#normalizeLink ⇒ Object
Returns the value of attribute normalizeLink.
108 109 110 |
# File 'lib/motion-markdown-it/index.rb', line 108 def normalizeLink @normalizeLink end |
#normalizeLinkText ⇒ Object
Returns the value of attribute normalizeLinkText.
109 110 111 |
# File 'lib/motion-markdown-it/index.rb', line 109 def normalizeLinkText @normalizeLinkText end |
#options ⇒ Object
Returns the value of attribute options.
106 107 108 |
# File 'lib/motion-markdown-it/index.rb', line 106 def @options end |
#renderer ⇒ Object
Returns the value of attribute renderer.
105 106 107 |
# File 'lib/motion-markdown-it/index.rb', line 105 def renderer @renderer end |
#validateLink ⇒ Object
Returns the value of attribute validateLink.
107 108 109 |
# File 'lib/motion-markdown-it/index.rb', line 107 def validateLink @validateLink end |
Instance Method Details
#configure(presets) ⇒ Object
chainable, internal MarkdownIt.configure(presets)
Batch load of all options and compenent settings. This is internal method, and you probably will not need it. But if you with - see available presets and data structure [here](github.com/markdown-it/markdown-it/tree/master/lib/presets)
We strongly recommend to use presets instead of direct config loads. That will give better compatibility with next versions.
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/motion-markdown-it/index.rb', line 331 def configure(presets) raise(ArgumentError, 'Wrong `markdown-it` preset, can\'t be empty') unless presets unless presets.is_a? Hash presetName = presets.to_sym presets = CONFIG[presetName] raise(ArgumentError, "Wrong `markdown-it` preset #{presetName}, check name") unless presets end self.set(presets[:options]) if presets[:options] if presets[:components] presets[:components].each_key do |name| if presets[:components][name][:rules] self.send(name).ruler.enableOnly(presets[:components][name][:rules]) end end end return self end |
#disable(list, ignoreInvalid) ⇒ Object
chainable MarkdownIt.disable(list, ignoreInvalid)
-
list (String|Array): rule name or list of rule names to disable.
-
ignoreInvalid (Boolean): set ‘true` to ignore errors when rule not found.
The same as [[MarkdownIt.enable]], but turn specified rules off.
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/motion-markdown-it/index.rb', line 394 def disable(list, ignoreInvalid) result = [] list = [ list ] if !list.is_a? Array result << @core.ruler.disable(list, true) result << @block.ruler.disable(list, true) result << @inline.ruler.disable(list, true) missed = list.select {|name| result.include?(name) } if missed.length && !ignoreInvalid raise StandardError, "MarkdownIt. Failed to disable unknown rule(s): #{missed}" end return self end |
#enable(list, ignoreInvalid) ⇒ Object
chainable MarkdownIt.enable(list, ignoreInvalid)
-
list (String|Array): rule name or list of rule names to enable
-
ignoreInvalid (Boolean): set ‘true` to ignore errors when rule not found.
Enable list or rules. It will automatically find appropriate components, containing rules with given names. If rule not found, and ‘ignoreInvalid` not set - throws exception.
##### Example
“‘javascript var md = require(’markdown-it’)()
.enable(['sub', 'sup'])
.disable('smartquotes');
“‘
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/motion-markdown-it/index.rb', line 369 def enable(list, ignoreInvalid) result = [] list = [ list ] if !list.is_a? Array result << @core.ruler.enable(list, true) result << @block.ruler.enable(list, true) result << @inline.ruler.enable(list, true) missed = list.select {|name| result.include?(name) } if missed.length && !ignoreInvalid raise StandardError, "MarkdownIt. Failed to enable unknown rule(s): #{missed}" end return self end |
#parse(src, env) ⇒ Object
internal MarkdownIt.parse(src, env) -> Array
-
src (String): source string
-
env (Object): environment sandbox
Parse input string and returns list of block tokens (special token type “inline” will contain list of inline tokens). You should not call this method directly, until you write custom renderer (for example, to produce AST).
‘env` is used to pass data between “distributed” rules and return additional metadata like reference info, needed for for renderer. It also can be used to inject data in specific cases. Usually, you will be ok to pass `{}`, and then pass updated object to renderer.
451 452 453 454 455 |
# File 'lib/motion-markdown-it/index.rb', line 451 def parse(src, env) state = RulesCore::StateCore.new(src, self, env) @core.process(state) return state.tokens end |
#parseInline(src, env) ⇒ Object
internal MarkdownIt.parseInline(src, env) -> Array
-
src (String): source string
-
env (Object): environment sandbox
The same as [[MarkdownIt.parse]] but skip all block rules. It returns the block tokens list with the single ‘inline` element, containing parsed inline tokens in `children` property. Also updates `env` object.
487 488 489 490 491 492 |
# File 'lib/motion-markdown-it/index.rb', line 487 def parseInline(src, env) state = RulesCore::StateCore.new(src, self, env) state.inlineMode = true @core.process(state) return state.tokens end |
#render(src, env = {}) ⇒ Object
MarkdownIt.render(src [, env]) -> String
-
src (String): source string
-
env (Object): environment sandbox
Render markdown string into html. It does all magic for you :).
‘env` can be used to inject additional metadata (`{}` by default). But you will not need it with high probability. See also comment in [[MarkdownIt.parse]].
467 468 469 470 471 |
# File 'lib/motion-markdown-it/index.rb', line 467 def render(src, env = {}) # self.parse(src, { references: {} }).each {|token| pp token.to_json} return @renderer.render(parse(src, env), @options, env) end |
#renderInline(src, env = {}) ⇒ Object
MarkdownIt.renderInline(src [, env]) -> String
-
src (String): source string
-
env (Object): environment sandbox
Similar to [[MarkdownIt.render]] but for single paragraph content. Result will NOT be wrapped into ‘<p>` tags.
502 503 504 |
# File 'lib/motion-markdown-it/index.rb', line 502 def renderInline(src, env = {}) return @renderer.render(parseInline(src, env), @options, env) end |
#set(options) ⇒ Object
chainable MarkdownIt.set(options)
Set parser options (in the same format as in constructor). Probably, you will never need it, but you can change options after constructor call.
##### Example
“‘javascript var md = require(’markdown-it’)()
.set({ html: true, breaks: true })
.set({ typographer, true });
“‘
Note: To achieve the best possible performance, don’t modify a ‘markdown-it` instance options on the fly. If you need multiple configurations it’s best to create multiple instances and initialize each with separate config.
315 316 317 318 |
# File 'lib/motion-markdown-it/index.rb', line 315 def set() assign(@options, ) return self end |
#to_html(src, env = {}) ⇒ Object
474 475 476 |
# File 'lib/motion-markdown-it/index.rb', line 474 def to_html(src, env = {}) render(src, env) end |
#use(plugin, *args) ⇒ Object
chainable MarkdownIt.use(plugin, params)
Load specified plugin with given params into current parser instance. It’s just a sugar to call ‘plugin(md, params)` with curring.
##### Example
“‘javascript var iterator = require(’markdown-it-for-inline’); var md = require(‘markdown-it’)()
.use(iterator, 'foo_replace', 'text', function (tokens, idx) {
tokens[idx].content = tokens[idx].content.replace(/foo/g, 'bar');
});
“‘
428 429 430 431 432 433 |
# File 'lib/motion-markdown-it/index.rb', line 428 def use(plugin, *args) puts "Implement: Parser.use" # var args = [ this ].concat(Array.prototype.slice.call(arguments, 1)); # plugin.call(plugin, args) # return self end |