Skip to content

Commit

Permalink
Fix positional info on tokens, nodes, also for tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed May 13, 2023
1 parent d6ac084 commit 69d77b6
Show file tree
Hide file tree
Showing 7 changed files with 591 additions and 107 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
],
"devDependencies": {
"@types/acorn": "^4.0.0",
"@types/estree": "^1.0.0",
"@types/node": "^20.0.0",
"acorn": "^8.0.0",
"acorn-jsx": "^5.0.0",
"c8": "^7.0.0",
"micromark": "^3.0.0",
"micromark-build": "^1.0.0",
"micromark-util-character": "^1.0.0",
"micromark-util-symbol": "^1.0.0",
"micromark-util-types": "^1.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
* `micromark-extension-mdx-jsx` to prohobit empty attribute values).
*/

import {ok as assert} from 'uvu/assert'
import {factoryMdxExpression} from 'micromark-factory-mdx-expression'
import {factorySpace} from 'micromark-factory-space'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {codes} from 'micromark-util-symbol/codes.js'
import {types} from 'micromark-util-symbol/types.js'
import {ok as assert} from 'uvu/assert'

/**
* Create an extension for `micromark` to enable MDX expression syntax.
Expand Down Expand Up @@ -111,6 +111,7 @@ export function mdxExpression(options) {
*/
function start(code) {
// To do: in `markdown-rs`, constructs need to parse the indent themselves.
// This should also be introduced in `micromark-js`.
assert(code === codes.leftCurlyBrace, 'expected `{`')
return before(code)
}
Expand Down
62 changes: 24 additions & 38 deletions packages/micromark-factory-mdx-expression/dev/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,29 @@

/**
* @typedef MdxSignalOk
* Good result.
* @property {'ok'} type
* Type.
* @property {Program | undefined} estree
* Value.
*
* @typedef MdxSignalNok
* Bad result.
* @property {'nok'} type
* Type.
* @property {VFileMessage} message
*
* @typedef MdxSignalEof
* Currently not given back.
* @property {'eof'} type
* @property {VFileMessage} message
* Value.
*
* @typedef {MdxSignalOk | MdxSignalNok} MdxSignal
*/

import {ok as assert} from 'uvu/assert'
import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
import {markdownLineEnding} from 'micromark-util-character'
import {eventsToAcorn} from 'micromark-util-events-to-acorn'
import {codes} from 'micromark-util-symbol/codes.js'
import {types} from 'micromark-util-symbol/types.js'
import {constants} from 'micromark-util-symbol/constants.js'
import {factorySpace} from 'micromark-factory-space'
import {positionFromEstree} from 'unist-util-position-from-estree'
import {ok as assert} from 'uvu/assert'
import {VFileMessage} from 'vfile-message'
import {eventsToAcorn} from 'micromark-util-events-to-acorn'

/**
* @this {TokenizeContext}
Expand Down Expand Up @@ -232,34 +231,21 @@ export function factoryMdxExpression(
)
}

if (markdownSpace(code)) {
// Idea: investigate if we’d need to use more complex stripping.
// Take this example:
//
// ```markdown
// > aaa <b c={`
// > d
// > `} /> eee
// ```
//
// Currently, the “paragraph” starts at `> | aaa`, so for the next line
// here we split it into `>␠|␠␠␠␠|␠d` (prefix, this indent here,
// expression data).
// The intention above is likely for the split to be as `>␠␠|␠␠␠␠|d`,
// which is impossible, but we can mimick it with `>␠|␠␠␠␠␠|d`.
//
// To improve the situation, we could take `tokenizer.line_start` at
// the start of the expression and move past whitespace.
// For future lines, we’d move at most to
// `line_start_shifted.column + 4`.
return factorySpace(
effects,
before,
types.linePrefix,
constants.tabSize
)(code)
}

// Idea: investigate if we’d need to use more complex stripping.
// Take this example:
//
// ```markdown
// > aaa <b c={`
// > d
// > `} /> eee
// ```
//
// The block quote takes one space from each line, the paragraph doesn’t.
// The intent above is *perhaps* for the split to be as `>␠␠|␠␠␠␠|d`,
// Currently, we *don’t* do anything at all, it’s `>␠|␠␠␠␠␠|d` instead.
//
// Note: we used to have some handling here, and `markdown-rs` still does,
// which should be removed.
return before(code)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/micromark-factory-mdx-expression/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"default": "./index.js"
},
"dependencies": {
"micromark-factory-space": "^1.0.0",
"@types/estree": "^1.0.0",
"micromark-util-character": "^1.0.0",
"micromark-util-events-to-acorn": "^1.0.0",
"micromark-util-symbol": "^1.0.0",
Expand Down
Loading

0 comments on commit 69d77b6

Please sign in to comment.