Skip to content

Commit

Permalink
feat: add virtual space nodes and improve linebreak positioning (#1)
Browse files Browse the repository at this point in the history
- virtual spaces are spaces that were expanded from tabs
- virutal spaces are represented in the syntax tree only if they should be rendered in the HTML output
- the idea of virtual spaces is borrowed from [`micromark/common-markup-state-machine`](https://github.com/micromark/common-markup-state-machine#cvs)
- linebreaks in indented code block, fenced code block, and html blocks are now more accurate so that we could grab their value contents more easily
  • Loading branch information
ikatyang authored Oct 7, 2019
1 parent ec8e9fd commit 4742ce0
Show file tree
Hide file tree
Showing 14 changed files with 17,399 additions and 16,118 deletions.
34 changes: 34 additions & 0 deletions corpus/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,40 @@ is-autolink www.su_b.example.com
(soft_line_break)
(www_autolink)))

================================================================================
Virtual space
================================================================================
- three virtual spaces

- ```
two virtual spaces
```

- <script>
/* one virtual space */</script>

--------------------------------------------------------------------------------

(document
(loose_list
(list_item
(list_marker)
(indented_code_block
(virtual_space)
(virtual_space)
(virtual_space)))
(list_item
(list_marker)
(fenced_code_block
(virtual_space)
(virtual_space)
(code_fence_content)))
(list_item
(list_marker)
(html_block
(line_break)
(virtual_space)))))

================================================================================
Unnamed 1
================================================================================
Expand Down
12 changes: 9 additions & 3 deletions corpus/spec.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ Example 5 - https://github.github.com/gfm/#example-5
(list_item
(list_marker)
(paragraph)
(indented_code_block))))
(indented_code_block
(virtual_space)
(virtual_space)))))

================================================================================
Example 6 - https://github.github.com/gfm/#example-6
Expand All @@ -71,7 +73,9 @@ Example 6 - https://github.github.com/gfm/#example-6

(document
(block_quote
(indented_code_block)))
(indented_code_block
(virtual_space)
(virtual_space))))

================================================================================
Example 7 - https://github.github.com/gfm/#example-7
Expand All @@ -84,7 +88,9 @@ Example 7 - https://github.github.com/gfm/#example-7
(tight_list
(list_item
(list_marker)
(indented_code_block))))
(indented_code_block
(virtual_space)
(virtual_space)))))

================================================================================
Example 8 - https://github.github.com/gfm/#example-8
Expand Down
7 changes: 5 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = grammar({
$._lnk_ref_def_end_mkr,
$._stx_bgn, $._stx_end_mkr,
$._atx_bgn, $._atx_end_mkr,
$._ind_cod_bgn_pfx,
$._ind_cod_bgn_mkr, $._ind_cod_end_mkr,
$._fen_cod_bgn, $._fen_cod_end, $._fen_cod_end_mkr,
$._htm_blk_scr_bgn, $._htm_blk_scr_end, $._htm_blk_scr_end_mkr,
Expand Down Expand Up @@ -68,13 +69,15 @@ module.exports = grammar({
$._bnk_lbk,
$._lit_lbk,
$._txt,

$.virtual_space,
],

conflicts: $ => [
[$._chk_box_pgh_ctn, $._chk_box_lik_stx_ctn], // requires 2 lookahead tokens to distinguish them
],

extras: $ => [$._lka, $._lit_lbk],
extras: $ => [$._lka, $._lit_lbk, $.virtual_space],

rules: {
document: $ => seq(repeat(choice($._blk_nod, $._bnk_lbk)), $._eof),
Expand All @@ -91,7 +94,7 @@ module.exports = grammar({
_stx_hed: $ => seq(alias($._pgh_hed, $.heading_content), $._pgh_end_mkr, $._stx_bgn),
_atx: $ => seq($._atx_hed, $._atx_end_mkr),
_atx_hed: $ => seq($._atx_bgn, alias(repeat($._inl_nod), $.heading_content), optional($._atx_end)),
_ind_cod: $ => seq($._ind_cod_hed, $._ind_cod_end_mkr),
_ind_cod: $ => seq($._ind_cod_bgn_pfx, $._ind_cod_hed, $._ind_cod_end_mkr),
_ind_cod_hed: $ => seq($._ind_cod_bgn_mkr, repeat(choice($._txt, $._blk_lbk))),
_fen_cod: $ => seq($._fen_cod_hed, $._fen_cod_end_mkr),
_fen_cod_hed: $ => seq($._fen_cod_bgn, optional(seq($._fen_cod_inf_bgn_mkr, alias(repeat(choice($._inl_txt)), $.info_string), $._fen_cod_inf_end_mkr)), optional(seq($._blk_lbk, alias(repeat(choice($._txt, alias($._blk_lbk, $.line_break))), $.code_fence_content))), optional(seq($._blk_lbk, $._fen_cod_end))),
Expand Down
16 changes: 16 additions & 0 deletions src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4742ce0

Please sign in to comment.