Vitaly Puzrin
10 years ago
28 changed files with 375 additions and 240 deletions
@ -1,14 +0,0 @@ |
|||
// Default options
|
|||
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = { |
|||
html: false, |
|||
xhtml: false, |
|||
breaks: false, |
|||
maxLevel: 20, |
|||
langPrefix: 'language-', |
|||
typograph: false, |
|||
highlight: function (/*str*/) { return ''; } |
|||
}; |
@ -0,0 +1,19 @@ |
|||
// Commonmark default options
|
|||
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = { |
|||
html: true, // Enable html tags in source
|
|||
xhtmlOut: true, // Use '/' to close single tags (<br />)
|
|||
breaks: false, // Convert '\n' in paragraphs into <br>
|
|||
langPrefix: 'language-', // CSS language prefix for fenced blocks
|
|||
linkify: false, // autoconvert url-like texts to links
|
|||
typographer: false, // Enable smartypants and other sweet transforms
|
|||
|
|||
// Highlighter function. Should return escaped html,
|
|||
// or '' if input not changed
|
|||
highlight: function (/*str, , lang*/) { return ''; }, |
|||
|
|||
maxNesting: 20 // Internal protection, recursion limit
|
|||
}; |
@ -0,0 +1,26 @@ |
|||
// List of active rules for strict commonmark mode
|
|||
|
|||
module.exports.block = [ |
|||
'code', |
|||
'blockquote', |
|||
'fences', |
|||
'heading', |
|||
'hr', |
|||
'htmlblock', |
|||
'lheading', |
|||
'list', |
|||
'paragraph' |
|||
]; |
|||
|
|||
module.exports.inline = [ |
|||
'autolink', |
|||
'backticks', |
|||
'emphasis', |
|||
'entity', |
|||
'escape', |
|||
'escape_html_char', |
|||
'htmltag', |
|||
'links', |
|||
'newline', |
|||
'text' |
|||
]; |
@ -0,0 +1,19 @@ |
|||
// Remarkable default options
|
|||
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = { |
|||
html: false, // Enable html tags in source
|
|||
xhtmlOut: false, // Use '/' to close single tags (<br />)
|
|||
breaks: false, // Convert '\n' in paragraphs into <br>
|
|||
langPrefix: 'language-', // CSS language prefix for fenced blocks
|
|||
linkify: false, // autoconvert url-like texts to links
|
|||
typographer: false, // Enable smartypants and other sweet transforms
|
|||
|
|||
// Highlighter function. Should return escaped html,
|
|||
// or '' if input not changed
|
|||
highlight: function (/*str, , lang*/) { return ''; }, |
|||
|
|||
maxNesting: 20 // Internal protection, recursion limit
|
|||
}; |
@ -0,0 +1,18 @@ |
|||
// Default typograph options
|
|||
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = { |
|||
singleQuotes: '‘’', |
|||
doubleQuotes: '“”', // «» - russian, „“ - deutch
|
|||
copyright: true, |
|||
trademark: true, |
|||
registered: true, |
|||
plusminus: true, |
|||
paragraph: true, |
|||
ellipsis: true, |
|||
dupes: true, |
|||
emDashes: true, |
|||
linkify: true |
|||
}; |
@ -0,0 +1,98 @@ |
|||
// Replace link-like texts with link nodes.
|
|||
//
|
|||
// Currently restricted to http/https/ftp
|
|||
//
|
|||
'use strict'; |
|||
|
|||
|
|||
var Autolinker = require('autolinker'); |
|||
var escapeHtml = require('../helpers').escapeHtml; |
|||
|
|||
|
|||
var links = []; |
|||
var autolinker = new Autolinker({ |
|||
stripPrefix: false, |
|||
replaceFn: function (autolinker, match) { |
|||
// Only collect matched strings but don't change anything.
|
|||
var url; |
|||
if (match.getType() === 'url') { |
|||
url = match.getUrl(); |
|||
if (/^(http|https|ftp|git)/.test(url)) { |
|||
links.push(url); |
|||
} |
|||
} |
|||
return false; |
|||
} |
|||
}); |
|||
|
|||
|
|||
module.exports = function linkify(t, state) { |
|||
var i, token, text, nodes, ln, pos, level, |
|||
tokens = state.tokens; |
|||
|
|||
for (i = tokens.length - 1; i >= 0; i--) { |
|||
token = tokens[i]; |
|||
|
|||
// Skip content of links
|
|||
if (token.type === 'link_close') { |
|||
i--; |
|||
while (tokens[i].type !== 'link_open' && tokens[i].level !== token.level) { |
|||
i--; |
|||
} |
|||
i--; |
|||
continue; |
|||
} |
|||
|
|||
if (token.type === 'text' && |
|||
(token.content.indexOf('://') || |
|||
token.content.indexOf('www'))) { |
|||
text = token.content; |
|||
links = []; |
|||
autolinker.link(text); |
|||
|
|||
if (!links.length) { continue; } |
|||
|
|||
// Now split string to nodes
|
|||
nodes = []; |
|||
level = token.level; |
|||
|
|||
for (ln = 0; ln < links.length; ln++) { |
|||
pos = text.indexOf(links[ln]); |
|||
if (pos) { |
|||
level = level; |
|||
nodes.push({ |
|||
type: 'text', |
|||
content: text.slice(0, pos), |
|||
level: level |
|||
}); |
|||
} |
|||
nodes.push({ |
|||
type: 'link_open', |
|||
href: links[ln], |
|||
title: '', |
|||
level: level++ |
|||
}); |
|||
nodes.push({ |
|||
type: 'text', |
|||
content: escapeHtml(links[ln]), |
|||
level: level |
|||
}); |
|||
nodes.push({ |
|||
type: 'link_close', |
|||
level: --level |
|||
}); |
|||
text = text.slice(pos + links[ln].length); |
|||
} |
|||
if (text.length) { |
|||
nodes.push({ |
|||
type: 'text', |
|||
content: text, |
|||
level: level |
|||
}); |
|||
} |
|||
|
|||
// replace cuttent node
|
|||
state.tokens = tokens = [].concat(tokens.slice(0, i), nodes, tokens.slice(i + 1)); |
|||
} |
|||
} |
|||
}; |
@ -0,0 +1,52 @@ |
|||
// Simple typographyc replacements
|
|||
//
|
|||
'use strict'; |
|||
|
|||
|
|||
module.exports = function replace(t, state) { |
|||
var i, token, text, |
|||
tokens = state.tokens, |
|||
options = t.options; |
|||
|
|||
for (i = tokens.length - 1; i >= 0; i--) { |
|||
token = tokens[i]; |
|||
if (token.type === 'text') { |
|||
text = token.content; |
|||
|
|||
if (text.indexOf('(') >= 0) { |
|||
if (options.copyright) { |
|||
text = text.replace(/\(c\)/gi, '©'); |
|||
} |
|||
if (options.trademark) { |
|||
text = text.replace(/\(tm\)/gi, '™'); |
|||
} |
|||
if (options.registered) { |
|||
text = text.replace(/\(r\)/gi, '®'); |
|||
} |
|||
if (options.paragraph) { |
|||
text = text.replace(/\(p\)/gi, '§'); |
|||
} |
|||
} |
|||
|
|||
if (options.plusminus && text.indexOf('+-') >= 0) { |
|||
text = text.replace(/\+-/g, '±'); |
|||
} |
|||
if (options.ellipsis && text.indexOf('..') >= 0) { |
|||
// .., ..., ....... -> …
|
|||
// but ?..... & !..... -> ?.. & !..
|
|||
text = text.replace(/\.{2,}/g, '…').replace(/([?!])…/g, '$1..'); |
|||
} |
|||
if (options.dupes && |
|||
(text.indexOf('????') >= 0 || |
|||
text.indexOf('!!!!') >= 0 || |
|||
text.indexOf(',,') >= 0)) { |
|||
text = text.replace(/([?!]){4,}/g, '$1$1$1').replace(/,{2,}/g, ','); |
|||
} |
|||
if (options.emDashes && text.indexOf('--') >= 0) { |
|||
text = text.replace(/(^|\s)--(\s|$)/mg, '$1—$2'); |
|||
} |
|||
|
|||
token.content = text; |
|||
} |
|||
} |
|||
}; |
Loading…
Reference in new issue