-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move processing to output function #1227
Conversation
lib/marked.js
Outdated
|
||
if (link) { | ||
href = link[1]; | ||
title = link[2].trim().slice(1, -1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you change the regex to ...\s+['"(](.*)['")]
then you wouldn't need this slice.
lib/marked.js
Outdated
href = href[0] === '<' ? href.substring(1, href.length - 1) : href; | ||
title = cap[3] ? cap[3].substring(1, cap[3].length - 1) : cap[3]; | ||
if (this.options.pedantic) { | ||
link = /^([^'"(]*[^\s])\s+(['"(].*['")])/.exec(href); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added (
and )
to the list of valid ways to denote a title based on the CommonMark spec. They were not in the original regex. Do we have a test for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if we actually want that since this is pedantic. I'm not sure where the pedantic specs are.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closest I've been able to find is from the Daring Fireball site:
https://duckduckgo.com/?q=site%3Adaringfireball.net+markdown&ia=web
https://daringfireball.net/projects/markdown/ - Download the Perl (I've never tried to read Perl)
https://daringfireball.net/projects/markdown/syntax
I was going to work on that after #1219 gets merged in.
We have the tests and assessment for the CommonMark and GFM specs. Next ones for me were going to be (still are), to finish migrating the old and the Perl.
@@ -762,8 +703,18 @@ InlineLexer.prototype.output = function(src) { | |||
src = src.substring(cap[0].length); | |||
this.inLink = true; | |||
href = cap[2]; | |||
href = href[0] === '<' ? href.substring(1, href.length - 1) : href; | |||
title = cap[3] ? cap[3].substring(1, cap[3].length - 1) : cap[3]; | |||
if (this.options.pedantic) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't test for pedantic much, and I don't see it well documented anywhere. The only docs I could find are in the man page.
Can you explain when I should test for pedantic in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand this code right, without pedantic
we will set href
but not title
. If my understanding is correct, why is this behavior desirable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pedantic
flag means follow the original 2004 spec from John Gruber (Daring Fireball).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-
The regexes are grouped based on the options. The link regex that you fixed was in the pedantic group
inline.pedantic = merge({}, inline.normal, {
-
The title is set in the else clause. The regex for a non-pedantic link is
/^!?\[(label)\]\(href(?:\s+(title))?\s*\)/
which is extremely complex when constructed and probably also error prone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here is the actual non-pedantic link regex:
/^!?\[((?:\[[^\[\]]*\]|\\[\[\]]?|`[^`]*`|[^\[\]\\])*?)\]\(\s*(<(?:\\[<>]?|[^\s<>\\])*>|(?:\\[()]?|\([^\s\x00-\x1f()\\]*\)|[^\s\x00-\x1f()\\])*?)(?:\s+("(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)))?\s*\)/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The link regex is checking for a title. If no title is found then the href is already set to the whole string inside the parentheses and there is no title.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex that you used before when there was no title (/^(<?[\s\S]*>?)/
) literally matches any string and since href
is already set to the whole string there is no need to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Further down we then set the title
field in the returned object based on the (undefined?) value of title. Do we want to set a default value for title
, e.g. ''
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My two cents would be an empty value of expected type. Avoid null and undefined checks and possibilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you are saying. Yes title should probably be defined as null
or ''
looks like commonmark doesn't differentiate between the title being an empty string or no title: demo
so I will set it to an empty string.
I removed the
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
move processing to output function
Marked version: 4711f6b
Description
Contributor
Committer
In most cases, this should be a different person than the contributor.