Skip to content

Commit 32d01be

Browse files
committed
Fixed regex and relative tests
1 parent b98a937 commit 32d01be

File tree

2 files changed

+162
-135
lines changed

2 files changed

+162
-135
lines changed

after/ftplugin/markdown.vim

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,33 @@ import autoload "../../lib/preview.vim"
55
import autoload "../../lib/links.vim"
66
import autoload '../../lib/utils.vim'
77

8-
# ------ Attempt for better regex -----------
9-
# The following regex reads (opening italics delimiter, pre and post):
10-
# 1. * must be preceded by a \W character with the exclusion of '*' and '\'
11-
# OR it must be at the beginning of line,
12-
# 2. * must NOT be followed by '\'s or by another '*'.
13-
# const ITALIC_OPEN_REGEX = '\v((\W|^|\\\*_)@<=(\*|\\)@<!)\*(\s|\*)@!'
14-
# USE THIS if you use bundled markdown:
15-
# searchpos('\v((\W|^)@<=(\\)@<!)\*(\s|\*)@!', 'b')
16-
#
17-
# The following regex reads (closing italics delimiter, pre and post):
18-
# 1. * cannot be preceded by a '\s' character or by a '\' or by a '*'.
19-
# 2. * cannot be followed by another '*'.
20-
# TODO: It won't catch closing delimiters when '\**' (the second asterisk is
21-
# supposed to be the closing delimiter) because a preceding '*' is
22-
# is already excluded.
23-
# const ITALIC_CLOSE_REGEX = '\v(\s|\\|\*)@<!\*\*@!' # ALMOST GOOD
24-
# ------ End attempt for better regex -----------
25-
268
# --------- Constants ---------------------------------
27-
const CODE_OPEN_REGEX = '\v(\\|`)@<!``@!\S'
28-
const CODE_CLOSE_REGEX = '\v\S(\\|`)@<!``@!'
29-
30-
const ITALIC_OPEN_REGEX = '\v((\\|\*)@<!|(\\\*))@<=\*\*@!\S'
31-
const ITALIC_CLOSE_REGEX = '\v\S((\\|\*)@<!|(\\\*))@<=\*\*@!'
32-
33-
const ITALIC_U_OPEN_REGEX = '\v((\\|_)@<!|(\\_))@<=_(_)@\S!'
34-
const ITALIC_U_CLOSE_REGEX = '\v\S((\\|_)@<!|(\\_))@<=_(_)@!'
9+
const CODE_OPEN_REGEX = '\v((\\|\`)@<!|(\\\`))@<=\`(\`)@!\S'
10+
const CODE_CLOSE_REGEX = '\v\S((\\|\`)@<!|(\\\`))@<=\`(\`)@!|^$'
11+
12+
# TODO It correctly pick \**, and it excludes ** and \*. It cannot distinguish
13+
# is the pattern *\* is opening or closing, because you can easily have
14+
# \S*\*\S, like foo*\*bar.
15+
# You cannot distinguish if the first * is an opening or closing pattern
16+
# without any additional information (read: internal state)
17+
# regex cannot be used when internal states are involved.
18+
# However, by using the information: A) The cursor is currently on a
19+
# highlighted region B) The search direction, you should reliably (always?)
20+
# hit the correct delimiter. Perhaps that could be mathematically proven.
21+
const ITALIC_OPEN_REGEX = '\v((\\|\*)@<!|(\\\*))@<=\*(\*)@!\S'
22+
const ITALIC_CLOSE_REGEX = '\v\S((\\|\*)@<!|(\\\*))@<=\*(\*)@!|^$'
23+
24+
const ITALIC_U_OPEN_REGEX = '\v((\\|_)@<!|(\\_))@<=_(_)@!\S'
25+
const ITALIC_U_CLOSE_REGEX = '\v\S((\\|_)@<!|(\\_))@<=_(_)@!|^$'
3526

3627
const BOLD_OPEN_REGEX = '\v((\\|\*)@<!|(\\\*))@<=\*\*(\*)@!\S'
37-
const BOLD_CLOSE_REGEX = '\v\S((\\|\*)@<!|(\\\*))@<=\*\*(\*)@!'
28+
const BOLD_CLOSE_REGEX = '\v\S((\\|\*)@<!|(\\\*))@<=\*\*(\*)@!|^$'
3829

39-
const BOLD_U_OPEN_REGEX = '\v((\\|_)@<!|(\\_))@<=__(_)@\S!'
40-
const BOLD_U_CLOSE_REGEX = '\v\S((\\|_)@<!|(\\_))@<=__(_)@!'
30+
const BOLD_U_OPEN_REGEX = '\v((\\|_)@<!|(\\_))@<=__(_)@!\S'
31+
const BOLD_U_CLOSE_REGEX = '\v\S((\\|_)@<!|(\\_))@<=__(_)@!|^$'
4132

42-
const STRIKE_OPEN_REGEX = '\v(\\|\~)@<!\~\~\~@!\S'
43-
const STRIKE_CLOSE_REGEX = '\v\S(\\|\~)@<!\~\~\~@!\S'
33+
const STRIKE_OPEN_REGEX = '\v((\\|\~)@<!|(\\\~))@<=\~\~(\~)@!\S'
34+
const STRIKE_CLOSE_REGEX = '\v\S((\\|\~)@<!|(\\\~))@<=\~\~(\~)@!|^$'
4435
# TODO: CODEBLOCK REGEX COULD BE IMPROVED
4536
const CODEBLOCK_REGEX = '```'
4637

@@ -55,6 +46,8 @@ const URL_PREFIXES = links.URL_PREFIXES
5546

5647
const LINK_OPEN_REGEX = '\v\zs\[\ze[^]]+\]'
5748
.. $'(\(({URL_PREFIXES}):[^)]+\)|\[[^]]+\])'
49+
# TODO Differently of the other CLOSE regexes, the link CLOSE regex end up ON
50+
# the last ] and not just before the match
5851
const LINK_CLOSE_REGEX = '\v\[[^]]+\zs\]\ze'
5952
.. $'(\(({URL_PREFIXES}):[^)]+\)|\[[^]]+\])'
6053

@@ -75,7 +68,7 @@ export const TEXT_STYLES_DICT = {
7568
open_regex: BOLD_U_OPEN_REGEX, close_regex: BOLD_U_CLOSE_REGEX },
7669

7770
markdownStrike: { open_delim: '~~', close_delim: '~~',
78-
open_regex: STRIKE_OPEN_REGEX, close_regex: STRIKE_OPEN_REGEX },
71+
open_regex: STRIKE_OPEN_REGEX, close_regex: STRIKE_CLOSE_REGEX },
7972
}
8073

8174
export const LINK_OPEN_DICT = {'[': LINK_OPEN_REGEX}

0 commit comments

Comments
 (0)