Skip to content

Commit

Permalink
Syntax: Refactor autolink contexts
Browse files Browse the repository at this point in the history
This commit is a major overhaul of autolink contexts

It addresses:
- sublimehq/Packages#1930
- sublimehq/Packages#2542
  • Loading branch information
deathaxe committed Sep 18, 2021
1 parent 16cd86f commit 8d36e15
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 42 deletions.
2 changes: 2 additions & 0 deletions messages/3.0.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ feedback you can use [GitHub issues][issues].
* Remove obsolete keymap selectors (required due to recent syntax changes)
* Add a macro to unbold bold italc text (required due to recent syntax changes)
* Only strip whitespace separated trailing hashes of headings from symbol lists
* Scope inet/email autolinks according to CommonMark 0.30.0

## New Features

* Scope path separators and escapes in urls
* Support fish fenced code (if supported syntax is installed)
* Partially support xonsh fenced code (use Python syntax due to a lack of xonsh syntax support in ST)

Expand Down
123 changes: 94 additions & 29 deletions syntaxes/Markdown.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ variables:
)
\s*$ # any amount of whitespace until EOL
)
# https://spec.commonmark.org/0.30/#email-autolink
email_domain_commonmark: '[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?'
email_user_commonmark: '[a-zA-Z0-9.!#$%&''*+/=?^_`{|}~-]+'

html_tag_open_commonmark: |-
(?xi:
<
Expand Down Expand Up @@ -733,8 +738,8 @@ contexts:
- include: code-span
- include: image-inline
- include: link-inline
- include: autolink-inet
- include: autolink-email
- include: autolink-inet
- include: image-ref
- include: link-ref-wiki
- include: link-ref-footnote
Expand Down Expand Up @@ -2435,13 +2440,29 @@ contexts:
- include: link-url-common

link-url-common:
- include: link-url-path-separators
- include: link-url-scheme-separators
- include: link-url-escapes
- include: paragraph-end

link-url-escapes:
- match: (%)\h{2}
scope: constant.character.escape.url.markdown
captures:
1: punctuation.definition.escape.markdown
- match: '[/&?#]|://'

link-url-path-separators:
- match: '[/&?#]'
scope: punctuation.separator.path.markdown

link-url-scheme-separators:
- match: ':/{,2}'
scope: punctuation.separator.path.markdown
- include: paragraph-end

link-url-scheme-separator:
- match: ':/{,2}'
scope: punctuation.separator.path.markdown
pop: true

###[ LINK/IMAGE/REFERENCE ATTRIBUTES ]########################################

Expand Down Expand Up @@ -2501,43 +2522,87 @@ contexts:
###[ AUTOLINKS ]##############################################################

autolink-email:
- match: (<)((?:mailto:)?[-+.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(>)
scope: meta.link.email.lt-gt.markdown
# CommonMark
# https://spec.commonmark.org/0.30/#email-autolink
- match: |-
(?x)
(<)
(
(?:mailto(:))?
{{email_user_commonmark}}
(@)
{{email_domain_commonmark}}(?:\.{{email_domain_commonmark}})*
)
(>)
captures:
0: meta.link.email.markdown
1: punctuation.definition.link.begin.markdown
2: markup.underline.link.markdown
4: punctuation.definition.link.end.markdown
- match: '[\w.+-]+@[\w-]+(\.((?![._-][\W])[\w_-])+)+(?![_-])'
scope: markup.underline.link.markdown
3: punctuation.separator.path.markdown
4: punctuation.separator.path.markdown
5: punctuation.definition.link.end.markdown
# Github Flavoured Markdown
- match: '[\w.+-]+(@)[\w-]+(?:\.(?:(?![._-][\W])[\w_-])+)+(?![_-])'
captures:
0: meta.link.email.markdown markup.underline.link.markdown
1: punctuation.separator.path.markdown

autolink-inet:
- match: (<)((?:https?|ftp)://.*?)(>)
scope: meta.link.inet.markdown
# CommonMark
# https://spec.commonmark.org/0.30/#autolinks
- match: <(?=[[:alpha:]][[:alnum:].+-]+:)
scope: punctuation.definition.link.begin.markdown
push:
- autolink-inet-angled-content
- link-url-scheme-separator
# Github Flavoured Markdown
# After a valid domain, zero or more non-space non-< characters may follow
- match: (?:(?:https|http|ftp)(://)|www\.)[\w-]+
captures:
1: punctuation.definition.link.begin.markdown
2: markup.underline.link.markdown
3: punctuation.definition.link.end.markdown
- match: (((https|http|ftp)://)|www\.)[\w-]+(\.[\w-]+)+
scope: markup.underline.link.markdown-gfm
push: autolink-inet-body
1: punctuation.separator.path.markdown
push: autolink-inet-unquoted-content

autolink-inet-body:
# After a valid domain, zero or more non-space non-< characters may follow
- match: (?=[?!.,:*_~]*(?:[\s<]|$)) # Trailing punctuation (specifically, ?, !, ., ,, :, *, _, and ~) will not be considered part of the autolink, though they may be included in the interior of the link
autolink-inet-angled-content:
- meta_scope: meta.link.inet.markdown
- meta_content_scope: markup.underline.link.markdown
- match: \>
scope: punctuation.definition.link.end.markdown
pop: true
- match: (?={{html_entity}}[?!.,:*_~]*[\s<]) # If an autolink ends in a semicolon (;), we check to see if it appears to resemble an entity reference; if the preceding text is & followed by one or more alphanumeric characters. If so, it is excluded from the autolink
# Spaces are not allowed in autolinks
- match: (?=\s)
pop: true
- match: \( # When an autolink ends in ), we scan the entire autolink for the total number of parentheses. If there is a greater number of closing parentheses than opening ones, we don’t consider the last character part of the autolink, in order to facilitate including an autolink inside a parenthesis
push:
- meta_scope: markup.underline.link.markdown-gfm
- match: (?=[?!.,:*_~]*[\s<])
pop: true
- match: \)
pop: true
- include: autolink-inet-common

autolink-inet-unquoted-content:
- meta_scope: meta.link.inet.markdown markup.underline.link.markdown-gfm
# Trailing punctuation (specifically, ?, !, ., ,, :, *, _, and ~) will not be
# considered part of the autolink, though they may be included in the interior
# of the link
- match: (?=[?!.,:*_~]*[\s<])
pop: true
# If an autolink ends in a semicolon (;), we check to see if it appears to
# resemble an entity reference; if the preceding text is & followed by one
# or more alphanumeric characters. If so, it is excluded from the autolink
- match: (?={{html_entity}})
pop: true
# When an autolink ends in ), we scan the entire autolink for the total number
# of parentheses. If there is a greater number of closing parentheses than
# opening ones, we don’t consider the last character part of the autolink,
# in order to facilitate including an autolink inside a parenthesis
- match: (?=\)[?!.,:*_~]*[\s<])
pop: true
- match: '[^?!.,:*_~\s<&()]+|\S'
scope: markup.underline.link.markdown-gfm
- match: \(
push: autolink-inet-group
- include: autolink-inet-common

autolink-inet-group:
- match: \)
pop: true
- include: autolink-inet-unquoted-content

autolink-inet-common:
- include: link-url-path-separators
- include: link-url-escapes

###[ TABLE ]##################################################################

Expand Down
Loading

0 comments on commit 8d36e15

Please sign in to comment.