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