-
Notifications
You must be signed in to change notification settings - Fork 152
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
Add mention links extension #171
base: master
Are you sure you want to change the base?
Changes from all commits
9c4e44a
a0a40d7
3969a6a
9f9463a
546cde4
d66cf59
89cf59a
4a0f9ed
3f355de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2709,7 +2709,7 @@ md_build_mark_char_map(MD_CTX* ctx) | |
if(ctx->parser.flags & MD_FLAG_LATEXMATHSPANS) | ||
ctx->mark_char_map['$'] = 1; | ||
|
||
if(ctx->parser.flags & MD_FLAG_PERMISSIVEEMAILAUTOLINKS) | ||
if ((ctx->parser.flags & MD_FLAG_MENTIONS) || (ctx->parser.flags & MD_FLAG_PERMISSIVEEMAILAUTOLINKS)) | ||
ctx->mark_char_map['@'] = 1; | ||
|
||
if(ctx->parser.flags & MD_FLAG_PERMISSIVEURLAUTOLINKS) | ||
|
@@ -3190,9 +3190,26 @@ md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, int n_lines, int table_mode) | |
continue; | ||
} | ||
|
||
/* A potential mention link. */ | ||
/* A potential permissive e-mail autolink. */ | ||
if(ch == _T('@')) { | ||
if(line->beg + 1 <= off && ISALNUM(off-1) && | ||
if( (ctx->parser.flags & MD_FLAG_MENTIONS) && (line->beg == off || (CH(off-1) == _T(' '))) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why only a space? I think any whitespace char would be ok here. Even maybe some listed (but likely not all) punctuation chars could validly proceed or follow respectivelly. I think things like the following can be common e.g. in a process of any collaborative document writing.
Or
|
||
{ | ||
OFF index = off + 1; | ||
if (index == line->end || CH(index) == ' ') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
off++; | ||
continue; | ||
} | ||
while (index <= line->end) | ||
{ | ||
if (!(ISALNUM(index) || (CH(index) == '_'))) | ||
break; | ||
index++; | ||
} | ||
PUSH_MARK('@', off, index, MD_MARK_RESOLVED); | ||
off = index; | ||
} | ||
else if(line->beg + 1 <= off && ISALNUM(off-1) && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This condition now should likely explicitly check whether are enabled |
||
off + 3 < line->end && ISALNUM(off+1)) | ||
{ | ||
PUSH_MARK(ch, off, off+1, MD_MARK_POTENTIAL_OPENER); | ||
|
@@ -4291,6 +4308,7 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines) | |
MD_FALLTHROUGH(); | ||
|
||
case '@': /* Permissive e-mail autolink. */ | ||
/* Mention link */ | ||
case ':': /* Permissive URL autolink. */ | ||
case '.': /* Permissive WWW autolink. */ | ||
{ | ||
|
@@ -4299,6 +4317,17 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines) | |
const CHAR* dest = STR(opener->end); | ||
SZ dest_size = closer->beg - opener->end; | ||
|
||
MD_SPAN_MENTION_DETAIL det; | ||
if (CH(mark->beg) == '@') | ||
{ | ||
det.text = (char *) ctx->text + mark->beg + 1; | ||
det.size = mark->end - mark->beg - 1; | ||
MD_ENTER_SPAN(MD_SPAN_MENTION, &det); | ||
MD_TEXT(text_type, STR(mark->beg), mark->end - mark->beg); | ||
MD_LEAVE_SPAN(MD_SPAN_MENTION, &det); | ||
break; | ||
} | ||
|
||
Comment on lines
+4320
to
+4330
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you know at this point whether it's a mention link or a permissive e-mail auto-link? Both extensions may be enabled at the same time. |
||
/* For permissive auto-links we do not know closer mark | ||
* position at the time of md_collect_marks(), therefore | ||
* it can be out-of-order in ctx->marks[]. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,7 +145,9 @@ typedef enum MD_SPANTYPE { | |
|
||
/* <u>...</u> | ||
* Note: Recognized only when MD_FLAG_UNDERLINE is enabled. */ | ||
MD_SPAN_U | ||
MD_SPAN_U, | ||
|
||
MD_SPAN_MENTION | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer renamimg it to |
||
} MD_SPANTYPE; | ||
|
||
/* Text is the actual textual contents of span. */ | ||
|
@@ -297,6 +299,12 @@ typedef struct MD_SPAN_WIKILINK { | |
MD_ATTRIBUTE target; | ||
} MD_SPAN_WIKILINK_DETAIL; | ||
|
||
/* Detailed info for MD_SPAN_MENTION. */ | ||
typedef struct MD_SPAN_MENTION { | ||
unsigned char size; | ||
MD_CHAR* text; | ||
Comment on lines
+304
to
+305
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand it's supposed to contain only a verbatim text (the username) with no possible nested formatting options, yet I wonder whether rather using |
||
} MD_SPAN_MENTION_DETAIL; | ||
|
||
/* Flags specifying extensions/deviations from CommonMark specification. | ||
* | ||
* By default (when MD_PARSER::flags == 0), we follow CommonMark specification. | ||
|
@@ -316,6 +324,7 @@ typedef struct MD_SPAN_WIKILINK { | |
#define MD_FLAG_LATEXMATHSPANS 0x1000 /* Enable $ and $$ containing LaTeX equations. */ | ||
#define MD_FLAG_WIKILINKS 0x2000 /* Enable wiki links extension. */ | ||
#define MD_FLAG_UNDERLINE 0x4000 /* Enable underline extension (and disables '_' for normal emphasis). */ | ||
#define MD_FLAG_MENTIONS 0x8000 /* Enable mention links extension. */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly please rename to |
||
|
||
#define MD_FLAG_PERMISSIVEAUTOLINKS (MD_FLAG_PERMISSIVEEMAILAUTOLINKS | MD_FLAG_PERMISSIVEURLAUTOLINKS | MD_FLAG_PERMISSIVEWWWAUTOLINKS) | ||
#define MD_FLAG_NOHTML (MD_FLAG_NOHTMLBLOCKS | MD_FLAG_NOHTMLSPANS) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Mention links | ||
|
||
With the flag `MD_FLAG_MENTIONS`, MD4C enables extension for recognition of mention (@) style links. | ||
|
||
A link ends when a non-alphanumeric character is hit. The only such character allowed is an underscore. | ||
|
||
```````````````````````````````` example | ||
someone said @me said such and such. | ||
. | ||
<p>someone said <x-mention data-target="me">@me</x-mention> said such and such.</p> | ||
```````````````````````````````` | ||
|
||
```````````````````````````````` example | ||
@me said such and such. | ||
. | ||
<p><x-mention data-target="me">@me</x-mention> said such and such.</p> | ||
```````````````````````````````` | ||
|
||
```````````````````````````````` example | ||
An empty at character is not recognized as a mention: | ||
|
||
@ | ||
|
||
@ | ||
. | ||
<p>An empty at character is not recognized as a mention:</p> | ||
<p>@</p> | ||
<p>@</p> | ||
```````````````````````````````` |
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.
Should likely call
render_verbatim()
instead. No entity translation should place here I guess.