summaryrefslogtreecommitdiffstats
path: root/packages/gitbook-markdown/lib/annotate_inline.js
blob: 469f4d820b6a49e399c6fe38e4a05252db392908 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var engine = require('./annotate_engine');

// Pulled from "kramed.InlineLexer.rules.gfm"
var rules = {
    escape: /^\\([\\`*{}\[\]()#$+\-.!_>~|])/,
    autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
    url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
    tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
    link: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\(\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*\)/,
    reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\s*\[([^\]]*)\]/,
    nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
    reffn: /^!?\[\^((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]/,
    strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
    em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
    code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
    br: /^ {2,}\n(?!\s*$)/,
    del: /^~~(?=\S)([\s\S]*?\S)~~/,
    text: /^[\s\S]+?(?=[\\<!\[_*`$~]|{%([\s]*)raw([\s]*)%}|{%([\s]*)endraw([\s]*)|https?:\/\/| {2,}\n|$)/,
    rawStart: /^{%([\s]*)raw([\s]*)%}/,
    rawEnd:  /^{%([\s]*)endraw([\s]*)%}/ ///[\s\S]*{%([\s]*)endraw([\s]*)%}/
    //_inside: /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/,
    //_href: /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/
};

// List of all the regexes we want to run
var ruleTypes = [
    'escape', 'autolink', 'url', 'tag', 'link', 'reflink',
    'nolink', 'reffn', 'strong', 'em', 'code', 'br',
    'del', 'rawStart', 'rawEnd', 'text'
];

// Mapping if rule type is different from token type
var ruleMap = {

};

function annotate(src) {
    return engine(src, rules, ruleTypes, ruleMap);
}

module.exports = annotate;