more compliant inline
This commit is contained in:
parent
2ab86665a5
commit
e9b864cd7c
@ -181,19 +181,36 @@ lex.token = function lex(str, tokens, line) {
|
||||
// but this was just easier for the time being.
|
||||
|
||||
var inline = function(str) {
|
||||
// img
|
||||
str = str.replace(
|
||||
/!\[([^\]]+)\]\(([^\s\)]+)\s*([^\)]*)\)/g,
|
||||
function(_, alt, src, title) {
|
||||
return '<img src="'
|
||||
+ src + '" alt="'
|
||||
+ alt + '"'
|
||||
+ (title
|
||||
? ' title="' + title + '"'
|
||||
: '')
|
||||
+ '>';
|
||||
var hash = ['#'];
|
||||
|
||||
str = str.replace(/#/g, '#0#');
|
||||
|
||||
str = str.replace(/`([^`]+)`/g, function(__, text) {
|
||||
text = '<code>' + escape(text) + '</code>';
|
||||
return '#' + (hash.push(text) - 1) + '#';
|
||||
});
|
||||
|
||||
// for <http://hello.world/> links
|
||||
str = str.replace(
|
||||
/<([^<>:\/ ]+:(?:\/\/)?[^>\n]+?|[^<>\n]+?(@)[^<>\n]+?)>/g,
|
||||
function(__, href, at) {
|
||||
if (at) {
|
||||
// according to the markdown "spec"
|
||||
// we need to mangle email addresses
|
||||
var href = mangle(href)
|
||||
, mail = mangle('mailto:') + href;
|
||||
return '<a href="' + mail + '">' + href + '</a>';
|
||||
}
|
||||
return '<a href="' + href + '">' + href + '</a>';
|
||||
}
|
||||
);
|
||||
|
||||
str = str.replace(/<[^\n>]+>/g, function(tag) {
|
||||
return '#' + (hash.push(tag) - 1) + '#';
|
||||
});
|
||||
|
||||
str = escape(str);
|
||||
|
||||
// links
|
||||
str = str.replace(
|
||||
/\[([^\]]+)\]\(([^\)]+)\)/g,
|
||||
@ -216,20 +233,18 @@ var inline = function(str) {
|
||||
}
|
||||
);
|
||||
|
||||
// for <http://hello.world/> links
|
||||
// img
|
||||
str = str.replace(
|
||||
/(?:<|<)([^<>:\/ ]+:(?:\/\/)?[^>\n]+?|[^<>\n]+?(@)[^<>\n]+?)(?:>|>)/g,
|
||||
function(__, href, at) {
|
||||
if (at) {
|
||||
// according to the markdown "spec"
|
||||
// we need to mangle email addresses
|
||||
var href = mangle(href)
|
||||
, mail = mangle('mailto:') + href;
|
||||
return '<a href="' + mail + '">' + href + '</a>';
|
||||
}
|
||||
return '<a href="' + href + '">' + href + '</a>';
|
||||
}
|
||||
);
|
||||
/!\[([^\]]+)\]\(([^\s\)]+)\s*([^\)]*)\)/g,
|
||||
function(_, alt, src, title) {
|
||||
return '<img src="'
|
||||
+ src + '" alt="'
|
||||
+ alt + '"'
|
||||
+ (title
|
||||
? ' title="' + title + '"'
|
||||
: '')
|
||||
+ '>';
|
||||
});
|
||||
|
||||
// strong
|
||||
str = str.replace(/__([^_]+)__/g, '<strong>$1</strong>');
|
||||
@ -239,14 +254,13 @@ var inline = function(str) {
|
||||
str = str.replace(/_([^_]+)_/g, '<em>$1</em>');
|
||||
str = str.replace(/\*([^*]+)\*/g, '<em>$1</em>');
|
||||
|
||||
// code
|
||||
str = str.replace(/`([^`]+)`/g, function(__, s) {
|
||||
return '<code>' + escape(s) + '</code>';
|
||||
});
|
||||
|
||||
// br
|
||||
str = str.replace(/ $/gm, '<br>');
|
||||
|
||||
str = str.replace(/#(\d+)#/g, function(__, i) {
|
||||
return hash[i];
|
||||
});
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
@ -293,6 +307,8 @@ var tok = function() {
|
||||
case 'list_item_start':
|
||||
var body = [];
|
||||
while (next().type !== 'list_item_end') {
|
||||
// TODO incorporate paragraph
|
||||
// list items here
|
||||
if (token.type === 'text') {
|
||||
body.push(inline(token.text));
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user