Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: syntax-tree/mdast-util-mdx-expression
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2.0.0
Choose a base ref
...
head repository: syntax-tree/mdast-util-mdx-expression
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2.0.1
Choose a head ref
  • 7 commits
  • 7 files changed
  • 1 contributor

Commits on Oct 19, 2023

  1. Update dev-dependencies

    wooorm committed Oct 19, 2023

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    3dcac1b View commit details

Commits on Aug 30, 2024

  1. Update dev-dependencies

    wooorm committed Aug 30, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    18d8d8f View commit details

Commits on Sep 13, 2024

  1. Add .tsbuildinfo to .gitignore

    wooorm committed Sep 13, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    7de410d View commit details
  2. Refactor to use @imports

    wooorm committed Sep 13, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    21a4d41 View commit details
  3. Add declaration maps

    wooorm committed Sep 13, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    48cbf69 View commit details
  4. Fix to add some indent in expressions like before

    wooorm committed Sep 13, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    8e5d290 View commit details
  5. 2.0.1

    wooorm committed Sep 13, 2024

    Verified

    This commit was signed with the committer’s verified signature.
    wooorm Titus
    Copy the full SHA
    54ed286 View commit details
Showing with 81 additions and 63 deletions.
  1. +8 −8 .github/workflows/main.yml
  2. +2 −0 .gitignore
  3. +20 −11 lib/index.js
  4. +13 −10 package.json
  5. +30 −29 readme.md
  6. +7 −5 test.js
  7. +1 −0 tsconfig.json
16 changes: 8 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
name: main
on:
- pull_request
- push
jobs:
main:
name: ${{matrix.node}}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{matrix.node}}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v3
- uses: codecov/codecov-action@v4
strategy:
matrix:
node:
- lts/gallium
- lts/hydrogen
- node
name: main
on:
- pull_request
- push
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
coverage/
node_modules/
*.d.ts.map
*.d.ts
*.log
*.tsbuildinfo
.DS_Store
yarn.lock
!/complex-types.d.ts
31 changes: 20 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/**
* @typedef {import('mdast-util-from-markdown').CompileContext} CompileContext
* @typedef {import('mdast-util-from-markdown').Extension} FromMarkdownExtension
* @typedef {import('mdast-util-from-markdown').Handle} FromMarkdownHandle
*
* @typedef {import('mdast-util-to-markdown').Handle} ToMarkdownHandle
* @typedef {import('mdast-util-to-markdown').Options} ToMarkdownExtension
*
* @typedef {import('../index.js').MdxFlowExpression} MdxFlowExpression
* @typedef {import('../index.js').MdxTextExpression} MdxTextExpression
* @import {CompileContext, Extension as FromMarkdownExtension, Handle as FromMarkdownHandle} from 'mdast-util-from-markdown'
* @import {MdxFlowExpression, MdxTextExpression} from 'mdast-util-mdx-expression'
* @import {Handle as ToMarkdownHandle, Options as ToMarkdownExtension, State} from 'mdast-util-to-markdown'
* @import {Parents} from 'mdast'
*/

import {ok as assert} from 'devlop'
@@ -104,8 +99,22 @@ function exitMdxExpressionData(token) {
/**
* @type {ToMarkdownHandle}
* @param {MdxFlowExpression | MdxTextExpression} node
* Node.
* @param {Parents | undefined} parent
* Parent, if any.
* @param {State} state
* Info passed around about the current state.
* @returns {string}
* Serialized markdown.
*/
function handleMdxExpression(node) {
function handleMdxExpression(node, parent, state) {
const value = node.value || ''
return '{' + value + '}'
const result = state.indentLines(value, function (line, index, blank) {
// Tab-size to eat has to be the same as what we serialize as.
// While in some places in markdown that’s 4, in JS it’s more common as 2.
// Which is what’s also in `mdast-util-mdx-jsx`:
// <https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/40b951b/lib/index.js#L52>
return (index === 0 || blank ? '' : ' ') + line
})
return '{' + result + '}'
}
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mdast-util-mdx-expression",
"version": "2.0.0",
"version": "2.0.1",
"description": "mdast extension to parse and serialize MDX (or MDX.js) expressions",
"license": "MIT",
"keywords": [
@@ -45,22 +45,22 @@
},
"devDependencies": {
"@types/acorn": "^4.0.0",
"@types/node": "^20.0.0",
"@types/node": "^22.0.0",
"acorn": "^8.0.0",
"c8": "^8.0.0",
"micromark-extension-mdx-expression": "^2.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"c8": "^10.0.0",
"micromark-extension-mdx-expression": "^3.0.0",
"prettier": "^3.0.0",
"remark-cli": "^12.0.0",
"remark-preset-wooorm": "^10.0.0",
"type-coverage": "^2.0.0",
"typescript": "^5.0.0",
"unist-util-remove-position": "^5.0.0",
"xo": "^0.54.0"
"xo": "^0.59.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"format": "remark . -qfo && prettier . -w --log-level warn && xo --fix",
"test-api-prod": "node --conditions production test.js",
"test-api-dev": "node --conditions development test.js",
"test-api": "npm run test-api-dev && npm run test-api-prod",
@@ -98,6 +98,9 @@
}
}
],
"prettier": true
"prettier": true,
"rules": {
"unicorn/prefer-at": "off"
}
}
}
59 changes: 30 additions & 29 deletions readme.md
Original file line number Diff line number Diff line change
@@ -12,27 +12,27 @@

## Contents

* [What is this?](#what-is-this)
* [When to use this](#when-to-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`mdxExpressionFromMarkdown()`](#mdxexpressionfrommarkdown)
* [`mdxExpressionToMarkdown()`](#mdxexpressiontomarkdown)
* [`MdxFlowExpression`](#mdxflowexpression)
* [`MdxTextExpression`](#mdxtextexpression)
* [`MdxFlowExpressionHast`](#mdxflowexpressionhast)
* [`MdxTextExpressionHast`](#mdxtextexpressionhast)
* [HTML](#html)
* [Syntax](#syntax)
* [Syntax tree](#syntax-tree)
* [Nodes](#nodes)
* [Content model](#content-model)
* [Types](#types)
* [Compatibility](#compatibility)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)
* [What is this?](#what-is-this)
* [When to use this](#when-to-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`mdxExpressionFromMarkdown()`](#mdxexpressionfrommarkdown)
* [`mdxExpressionToMarkdown()`](#mdxexpressiontomarkdown)
* [`MdxFlowExpression`](#mdxflowexpression)
* [`MdxTextExpression`](#mdxtextexpression)
* [`MdxFlowExpressionHast`](#mdxflowexpressionhast)
* [`MdxTextExpressionHast`](#mdxtextexpressionhast)
* [HTML](#html)
* [Syntax](#syntax)
* [Syntax tree](#syntax-tree)
* [Nodes](#nodes)
* [Content model](#content-model)
* [Types](#types)
* [Compatibility](#compatibility)
* [Related](#related)
* [Contribute](#contribute)
* [License](#license)

## What is this?

@@ -390,12 +390,13 @@ somewhere in your types, as that registers the new node types in the tree.

```js
/**
* @typedef {import('mdast-util-mdx-expression')}
* @import {} from 'mdast-util-mdx-expression'
* @import {Root} from 'mdast'
*/

import {visit} from 'unist-util-visit'

/** @type {import('mdast').Root} */
/** @type {Root} */
const tree = getMdastNodeSomeHow()

visit(tree, function (node) {
@@ -418,12 +419,12 @@ This utility works with `mdast-util-from-markdown` version 2+ and

## Related

* [`remarkjs/remark-mdx`][remark-mdx]
— remark plugin to support MDX
* [`syntax-tree/mdast-util-mdx`][mdast-util-mdx]
— mdast utility to support MDX
* [`micromark/micromark-extension-mdx-expression`][extension]
— micromark extension to parse MDX expressions
* [`remarkjs/remark-mdx`][remark-mdx]
— remark plugin to support MDX
* [`syntax-tree/mdast-util-mdx`][mdast-util-mdx]
— mdast utility to support MDX
* [`micromark/micromark-extension-mdx-expression`][extension]
— micromark extension to parse MDX expressions

## Contribute

12 changes: 7 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
)

await t.test(
'should support a flow expression (agnostic)',
'should support an indented flow expression (agnostic)',
async function () {
assert.deepEqual(
fromMarkdown('{\n 1 + 1\n}', {
@@ -62,7 +62,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
children: [
{
type: 'mdxFlowExpression',
value: '\n 1 + 1\n',
value: '\n1 + 1\n',
position: {
start: {line: 1, column: 1, offset: 0},
end: {line: 3, column: 2, offset: 11}
@@ -270,6 +270,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
removePosition(tree, {force: true})

// Cheap clone to remove non-JSON values.
// eslint-disable-next-line unicorn/prefer-structured-clone -- `JSON` to drop instances.
tree = JSON.parse(JSON.stringify(tree))

assert.deepEqual(tree, {
@@ -336,6 +337,7 @@ test('mdxExpressionFromMarkdown()', async function (t) {
removePosition(tree, {force: true})

// Cheap clone to remove non-JSON values.
// eslint-disable-next-line unicorn/prefer-structured-clone -- `JSON` to drop instances.
tree = JSON.parse(JSON.stringify(tree))

assert.deepEqual(tree, {
@@ -465,7 +467,7 @@ test('mdxExpressionToMarkdown()', async function (t) {
},
{extensions: [mdxExpressionToMarkdown()]}
),
'{a + b}\n\n{\nc +\n1\n}\n\n{}\n\nd\n'
'{a + b}\n\n{\n c +\n 1\n}\n\n{}\n\nd\n'
)
})

@@ -514,7 +516,7 @@ test('mdxExpressionToMarkdown()', async function (t) {

test('roundtrip', async function (t) {
await t.test(
'should *not* strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)',
'should strip superfluous whitespace depending on the opening prefix, when roundtripping expressions (flow)',
async function () {
assert.deepEqual(
toMarkdown(
@@ -524,7 +526,7 @@ test('roundtrip', async function (t) {
}),
{extensions: [mdxExpressionToMarkdown()]}
),
'{`\n a\n `}\n'
'{`\n a\n `}\n'
)
}
)
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
"compilerOptions": {
"checkJs": true,
"customConditions": ["development"],
"declarationMap": true,
"declaration": true,
"emitDeclarationOnly": true,
"exactOptionalPropertyTypes": true,