2014-10-22 9 views
8

Marked के साथ मैं कार्यान्वयन के दौरान लेक्सर नियमों को आसानी से ओवरराइड/जोड़/बदल सकता हूं, और यह बढ़िया है! उदाहरण के लिए मैं इस प्रकार से हैडर बनाने के लिए हैश के बीच की जगह का उपयोग करने के लिए एक पाठ पर हस्ताक्षर के लिए मजबूर कर सकते हैं:mark.js के लिए कस्टम InlineLexer नियम कैसे लिखें?

var lexer = new marked.Lexer(options); 
console.log(lexer); 
lexer.rules.heading = /^\s*(#{1,6})\s+([^\n]+?) *#* *(?:\n+|$)/ 

console.log(marked.parser(lexer.lex('#hashtag?'), options)); 
//<p>#hashtag?</p> 
console.log(marked.parser(lexer.lex('# heading?'), options)); 
//<h1 id="undefinedheading-">heading?</h1> 

कूल!

लेकिन क्या inlineLexer के लिए आसानी से ऐसा करने का कोई तरीका है? जैसा कि मुझे लोगों को अगली अनुक्रम के साथ एक छवि जोड़ने में सक्षम होना चाहिए: %[My Image](http://example.com/img.jpg)? इसलिए मैंने संशोधित किया:

var inlineLexer = marked.InlineLexer; 
inlineLexer.rules.link = /^[!%]{0,1}?\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\(\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*\)/; 
... 

मुझे आगे क्या करना चाहिए? एक चिह्नित उदाहरण के लिए एक कस्टम inlineLexer कैसे बांधें? कृपया मुझे यह दिखाने का एक उदाहरण दिखाएं कि यह कैसे करें! मैं कस्टम इनलाइन लेक्सर नियमों को कैसे संशोधित/जोड़ सकता हूं?

+1

कृपया इस [मुद्दा] देखो (https://github.com/chjj/marked/issues/504) जहां मैं अपने समाधान तैनात। – Rugal

उत्तर

4

मैंने लाइब्रेरी स्रोत को बदलने या वैश्विक चिह्नित उदाहरण या प्रोटोटाइप को प्रभावित किए बिना इनलाइन लेक्सर के कुछ अनुकूलन की अनुमति देने के लिए इसके हिस्सों को ओवरराइड करने का एक तरीका खोजने के लिए mark.js के स्रोत कोड को देखा है।

var renderer = new marked.Renderer(); 
var lexer = new marked.Lexer(); 
var parser = new marked.Parser(); 

var options = { 
    renderer: renderer, 
    gfm: true, 
    tables: false, 
    breaks: true, 
    pedantic: false, 
    sanitize: true, 
    smartLists: true, 
    smartypants: false 
} 

parser.inline = new marked.InlineLexer([], options); 
parser.inline.rules = angular.copy(parser.inline.rules); // deep copy, otherwise global marked will be affected 

parser.inline.rules.link = /^[!%]{0,1}?\[((?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*)\]\(\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*\)/; 
renderer.link = function(href, title, text) { 
    // this is the standard link renderer that can be altered if desired ... 
    if (this.options.sanitize) { 
     try { 
      var prot = decodeURIComponent(unescape(href)) 
       .replace(/[^\w:]/g, '') 
       .toLowerCase(); 
     } catch (e) { 
      return ''; 
     } 
     if (prot.indexOf('javascript:') === 0 || prot.indexOf('vbscript:') === 0) { 
      return ''; 
     } 
    } 
    var out = '<a href="' + href + '"'; 
    if (title) { 
     out += ' title="' + title + '"'; 
    } 
    out += '>' + text + '</a>'; 
    return out; 
} 

function parse(src) { 
    parser.inline.links = src.links; 
    parser.tokens = src.reverse(); 
    var out = ''; 
    while (parser.next()) { 
     out += parser.tok(); 
    } 
    return out; 
}; 

function parseText(text) { 
    var lex = lexer.lex(text); 
    var r = parse(lex); 
    return r; 
} 
+0

मैंने एनालर लेपीर नियमों की एक गहरी प्रतिलिपि बनाने के लिए कोणीय.copy() का उपयोग किया है, और क्योंकि मैं कोणीय का उपयोग करता हूं, लेकिन कोई भी गहरी प्रति ठीक होगी। –

+1

ग्रेट। मैं इसे आज़मा दूंगा। लेकिन सदियों पहले यह था। :) –

संबंधित मुद्दे