Skip to content

Commit 231f316

Browse files
authored
Merge pull request #97 from boltlessengineer/split_parser
Add wiki-links support (#54)
2 parents 41a8070 + 5d5284a commit 231f316

File tree

7 files changed

+487
-25
lines changed

7 files changed

+487
-25
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ of them are on by default, these can be disabled with the environment variable
3838
| YAML metadata | `EXTENSION_MINUS_METADATA` | [link](https://gohugo.io/content-management/front-matter/) || |
3939
| TOML metadata | `EXTENSION_PLUS_METADATA` | [link](https://gohugo.io/content-management/front-matter/) || |
4040
| Tags | `EXTENSION_TAGS` | [link](https://help.obsidian.md/Editing+and+formatting/Tags#Tag+format) | | |
41+
| Wiki Link | `EXTENSION_WIKI_LINK` | [link](https://help.obsidian.md/Linking+notes+and+files/Internal+links) | | |
4142

4243
## Usage in Editors
4344

common/grammar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ exports.EXTENSION_MINUS_METADATA = process.env.EXTENSION_MINUS_METADATA || expor
77
exports.EXTENSION_PLUS_METADATA = process.env.EXTENSION_PLUS_METADATA || exports.EXTENSION_DEFAULT || process.env.ALL_EXTENSIONS;
88
exports.EXTENSION_TAGS = process.env.EXTENSION_TAGS || process.env.ALL_EXTENSIONS;
99
exports.EXTENSION_LATEX = process.env.EXTENSION_LATEX || exports.EXTENSION_DEFAULT || process.env.ALL_EXTENSIONS;
10+
exports.EXTENSION_WIKI_LINK = process.env.EXTENSION_WIKI_LINK || process.env.ALL_EXTENSIONS;
1011

1112
const PUNCTUATION_CHARACTERS_REGEX = '!-/:-@\\[-`\\{-~';
1213
const PUNCTUATION_CHARACTERS_ARRAY = [
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
================================================================================
2+
Basic Wiki-link parsing.
3+
================================================================================
4+
[[Tree laws of motion]]
5+
6+
-------------------------------------------------------------------------------
7+
(inline
8+
(wiki_link
9+
(link_destination)
10+
))
11+
12+
================================================================================
13+
Wiki-link to a file
14+
================================================================================
15+
[[Figure 1.png]]
16+
17+
-------------------------------------------------------------------------------
18+
(inline
19+
(wiki_link
20+
(link_destination)
21+
))
22+
23+
================================================================================
24+
Wiki-link to a heading in a note
25+
================================================================================
26+
[[Tree laws of motion#Second law]]
27+
28+
-------------------------------------------------------------------------------
29+
(inline
30+
(wiki_link
31+
(link_destination)
32+
))
33+
34+
================================================================================
35+
Wiki-link with title
36+
================================================================================
37+
[[Internal links|custom display text]]
38+
39+
-------------------------------------------------------------------------------
40+
(inline
41+
(wiki_link
42+
(link_destination)
43+
(link_text)
44+
))
45+
46+
================================================================================
47+
Wiki-link opener with no closer
48+
================================================================================
49+
[[
50+
51+
-------------------------------------------------------------------------------
52+
(inline)
53+
54+
================================================================================
55+
Wiki-link version of Example 556
56+
================================================================================
57+
[[[foo]]]
58+
59+
--------------------------------------------------------------------------------
60+
61+
(inline
62+
(wiki_link
63+
(link_destination)))
64+

tree-sitter-markdown-inline/corpus/spec.txt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2909,17 +2909,6 @@ Example 555 - https://github.github.com/gfm/#example-555
29092909
(shortcut_link
29102910
(link_text)))
29112911

2912-
================================================================================
2913-
Example 556 - https://github.github.com/gfm/#example-556
2914-
================================================================================
2915-
[[[foo]]]
2916-
2917-
--------------------------------------------------------------------------------
2918-
2919-
(inline
2920-
(shortcut_link
2921-
(link_text)))
2922-
29232912
================================================================================
29242913
Example 557 - https://github.github.com/gfm/#example-557
29252914
================================================================================
@@ -3021,20 +3010,6 @@ Example 566 - https://github.github.com/gfm/#example-566
30213010

30223011
--------------------------------------------------------------------------------
30233012

3024-
(inline
3025-
(shortcut_link
3026-
(link_text
3027-
(emphasis
3028-
(emphasis_delimiter)
3029-
(emphasis_delimiter)))))
3030-
3031-
================================================================================
3032-
Example 567 - https://github.github.com/gfm/#example-567
3033-
================================================================================
3034-
[[*foo* bar]]
3035-
3036-
--------------------------------------------------------------------------------
3037-
30383013
(inline
30393014
(shortcut_link
30403015
(link_text

tree-sitter-markdown-inline/grammar.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ module.exports = grammar(add_inline_rules({
109109
[$.shortcut_link, $._link_text],
110110
[$.link_destination, $.link_title],
111111
[$._link_destination_parenthesis, $.link_title],
112+
113+
[$.wiki_link, $._inline_element],
114+
[$.wiki_link, $._inline_element_no_star],
115+
[$.wiki_link, $._inline_element_no_underscore],
116+
[$.wiki_link, $._inline_element_no_tilde],
112117
],
113118
extras: $ => [],
114119

@@ -184,6 +189,29 @@ module.exports = grammar(add_inline_rules({
184189
')'
185190
)),
186191

192+
wiki_link: $ => prec.dynamic(2 * PRECEDENCE_LEVEL_LINK, seq(
193+
'[', '[',
194+
alias($._wiki_link_destination, $.link_destination),
195+
optional(seq(
196+
'|',
197+
alias($._wiki_link_text, $.link_text)
198+
)),
199+
']', ']'
200+
)
201+
),
202+
203+
_wiki_link_destination: $ => repeat1(choice(
204+
$._word,
205+
common.punctuation_without($, ['[',']', '|']),
206+
$._whitespace,
207+
)),
208+
209+
_wiki_link_text: $ => repeat1(choice(
210+
$._word,
211+
common.punctuation_without($, ['[',']']),
212+
$._whitespace,
213+
)),
214+
187215
// Images work exactly like links with a '!' added in front.
188216
//
189217
// https://github.github.com/gfm/#images
@@ -407,8 +435,12 @@ function add_inline_rules(grammar) {
407435
$.full_reference_link,
408436
$.collapsed_reference_link,
409437
$.inline_link,
438+
// (common.EXTENSION_WIKI_LINK && $.wiki_link),
410439
seq(choice('[', ']'), optional($._last_token_punctuation)),
411440
]);
441+
if (common.EXTENSION_WIKI_LINK) {
442+
elements.push($.wiki_link);
443+
}
412444
}
413445
return choice(...elements);
414446
};

tree-sitter-markdown-inline/queries/highlights.scm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@
3535
(image ["!" "[" "]" "("] @punctuation.delimiter)
3636
(inline_link ["[" "]" "("] @punctuation.delimiter)
3737
(shortcut_link ["[" "]"] @punctuation.delimiter)
38+
(wiki_link ["[" "|" "]"] @punctuation.delimiter)

0 commit comments

Comments
 (0)