Skip to content

Commit 2a5ab92

Browse files
committed
.
0 parents  commit 2a5ab92

File tree

12 files changed

+2370
-0
lines changed

12 files changed

+2370
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.github/workflows/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: main
2+
on:
3+
- pull_request
4+
- push
5+
jobs:
6+
main:
7+
name: ${{matrix.node}}
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: dcodeIO/setup-node-nvm@master
12+
with:
13+
node-version: ${{matrix.node}}
14+
- run: npm install
15+
- run: npm test
16+
- uses: codecov/codecov-action@v1
17+
strategy:
18+
matrix:
19+
node:
20+
- lts/dubnium
21+
- node

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.nyc_output/
2+
coverage/
3+
node_modules/
4+
*.log
5+
.DS_Store
6+
yarn.lock

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
*.json
3+
*.md

from-markdown.js

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
exports.canContainEols = ['mdxJsxTextElement']
2+
exports.enter = {
3+
mdxJsxFlowTag: enterMdxJsxTag,
4+
mdxJsxFlowTagClosingMarker: enterMdxJsxTagClosingMarker,
5+
mdxJsxFlowTagAttribute: enterMdxJsxTagAttribute,
6+
mdxJsxFlowTagExpressionAttribute: enterMdxJsxTagExpressionAttribute,
7+
mdxJsxFlowTagAttributeValueLiteral: buffer,
8+
mdxJsxFlowTagAttributeValueExpression: buffer,
9+
mdxJsxFlowTagSelfClosingMarker: enterMdxJsxTagSelfClosingMarker,
10+
11+
mdxJsxTextTag: enterMdxJsxTag,
12+
mdxJsxTextTagClosingMarker: enterMdxJsxTagClosingMarker,
13+
mdxJsxTextTagAttribute: enterMdxJsxTagAttribute,
14+
mdxJsxTextTagExpressionAttribute: enterMdxJsxTagExpressionAttribute,
15+
mdxJsxTextTagAttributeValueLiteral: buffer,
16+
mdxJsxTextTagAttributeValueExpression: buffer,
17+
mdxJsxTextTagSelfClosingMarker: enterMdxJsxTagSelfClosingMarker
18+
}
19+
exports.exit = {
20+
mdxJsxFlowTagClosingMarker: exitMdxJsxTagClosingMarker,
21+
mdxJsxFlowTagNamePrimary: exitMdxJsxTagNamePrimary,
22+
mdxJsxFlowTagNameMember: exitMdxJsxTagNameMember,
23+
mdxJsxFlowTagNameLocal: exitMdxJsxTagNameLocal,
24+
mdxJsxFlowTagExpressionAttribute: exitMdxJsxTagExpressionAttribute,
25+
mdxJsxFlowTagExpressionAttributeValue: data,
26+
mdxJsxFlowTagAttributeNamePrimary: exitMdxJsxTagAttributeNamePrimary,
27+
mdxJsxFlowTagAttributeNameLocal: exitMdxJsxTagAttributeNameLocal,
28+
mdxJsxFlowTagAttributeValueLiteral: exitMdxJsxTagAttributeValueLiteral,
29+
mdxJsxFlowTagAttributeValueLiteralValue: data,
30+
mdxJsxFlowTagAttributeValueExpression: exitMdxJsxTagAttributeValueExpression,
31+
mdxJsxFlowTagAttributeValueExpressionValue: data,
32+
mdxJsxFlowTagSelfClosingMarker: exitMdxJsxTagSelfClosingMarker,
33+
mdxJsxFlowTag: exitMdxJsxTag,
34+
35+
mdxJsxTextTagClosingMarker: exitMdxJsxTagClosingMarker,
36+
mdxJsxTextTagNamePrimary: exitMdxJsxTagNamePrimary,
37+
mdxJsxTextTagNameMember: exitMdxJsxTagNameMember,
38+
mdxJsxTextTagNameLocal: exitMdxJsxTagNameLocal,
39+
mdxJsxTextTagExpressionAttribute: exitMdxJsxTagExpressionAttribute,
40+
mdxJsxTextTagExpressionAttributeValue: data,
41+
mdxJsxTextTagAttributeNamePrimary: exitMdxJsxTagAttributeNamePrimary,
42+
mdxJsxTextTagAttributeNameLocal: exitMdxJsxTagAttributeNameLocal,
43+
mdxJsxTextTagAttributeValueLiteral: exitMdxJsxTagAttributeValueLiteral,
44+
mdxJsxTextTagAttributeValueLiteralValue: data,
45+
mdxJsxTextTagAttributeValueExpression: exitMdxJsxTagAttributeValueExpression,
46+
mdxJsxTextTagAttributeValueExpressionValue: data,
47+
mdxJsxTextTagSelfClosingMarker: exitMdxJsxTagSelfClosingMarker,
48+
mdxJsxTextTag: exitMdxJsxTag
49+
}
50+
51+
var parseEntities = require('parse-entities')
52+
var stringifyPosition = require('unist-util-stringify-position')
53+
var VMessage = require('vfile-message')
54+
55+
function buffer() {
56+
this.buffer()
57+
}
58+
59+
function data(token) {
60+
this.config.enter.data.call(this, token)
61+
this.config.exit.data.call(this, token)
62+
}
63+
64+
function enterMdxJsxTag(token) {
65+
if (!this.getData('mdxJsxTagStack')) this.setData('mdxJsxTagStack', [])
66+
67+
this.setData('mdxJsxTag', {
68+
name: null,
69+
attributes: [],
70+
start: token.start,
71+
end: token.end
72+
})
73+
74+
this.buffer()
75+
}
76+
77+
function enterMdxJsxTagClosingMarker(token) {
78+
if (!this.getData('mdxJsxTagStack').length) {
79+
throw new VMessage(
80+
'Unexpected closing slash `/` in tag, expected an open tag first',
81+
{start: token.start, end: token.end},
82+
'mdast-util-mdx-jsx:unexpected-closing-slash'
83+
)
84+
}
85+
}
86+
87+
function enterMdxJsxTagAnyAttribute(token) {
88+
if (this.getData('mdxJsxTag').close) {
89+
throw new VMessage(
90+
'Unexpected attribute in closing tag, expected the end of the tag',
91+
{start: token.start, end: token.end},
92+
'mdast-util-mdx-jsx:unexpected-attribute'
93+
)
94+
}
95+
}
96+
97+
function enterMdxJsxTagSelfClosingMarker(token) {
98+
if (this.getData('mdxJsxTag').close) {
99+
throw new VMessage(
100+
'Unexpected self-closing slash `/` in closing tag, expected the end of the tag',
101+
{start: token.start, end: token.end},
102+
'mdast-util-mdx-jsx:unexpected-self-closing-slash'
103+
)
104+
}
105+
}
106+
107+
function exitMdxJsxTagClosingMarker() {
108+
this.getData('mdxJsxTag').close = true
109+
}
110+
111+
function exitMdxJsxTagNamePrimary(token) {
112+
this.getData('mdxJsxTag').name = this.sliceSerialize(token)
113+
}
114+
115+
function exitMdxJsxTagNameMember(token) {
116+
this.getData('mdxJsxTag').name += '.' + this.sliceSerialize(token)
117+
}
118+
119+
function exitMdxJsxTagNameLocal(token) {
120+
this.getData('mdxJsxTag').name += ':' + this.sliceSerialize(token)
121+
}
122+
123+
function enterMdxJsxTagAttribute(token) {
124+
enterMdxJsxTagAnyAttribute.call(this, token)
125+
this.getData('mdxJsxTag').attributes.push({
126+
type: 'mdxJsxAttribute',
127+
name: null,
128+
value: null
129+
})
130+
}
131+
132+
function enterMdxJsxTagExpressionAttribute(token) {
133+
enterMdxJsxTagAnyAttribute.call(this, token)
134+
this.getData('mdxJsxTag').attributes.push({
135+
type: 'mdxJsxExpressionAttribute',
136+
value: null
137+
})
138+
this.buffer()
139+
}
140+
141+
function exitMdxJsxTagExpressionAttribute(token) {
142+
var attributes = this.getData('mdxJsxTag').attributes
143+
attributes[attributes.length - 1].value = this.resume()
144+
145+
if (token.estree) {
146+
attributes[attributes.length - 1].data = {estree: token.estree}
147+
}
148+
}
149+
150+
function exitMdxJsxTagAttributeNamePrimary(token) {
151+
var attributes = this.getData('mdxJsxTag').attributes
152+
attributes[attributes.length - 1].name = this.sliceSerialize(token)
153+
}
154+
155+
function exitMdxJsxTagAttributeNameLocal(token) {
156+
var attributes = this.getData('mdxJsxTag').attributes
157+
attributes[attributes.length - 1].name += ':' + this.sliceSerialize(token)
158+
}
159+
160+
function exitMdxJsxTagAttributeValueLiteral() {
161+
var attributes = this.getData('mdxJsxTag').attributes
162+
attributes[attributes.length - 1].value = parseEntities(this.resume(), {
163+
nonTerminated: false
164+
})
165+
}
166+
167+
function exitMdxJsxTagAttributeValueExpression(token) {
168+
var attributes = this.getData('mdxJsxTag').attributes
169+
170+
attributes[attributes.length - 1].value = {
171+
type: 'mdxJsxAttributeValueExpression',
172+
value: this.resume()
173+
}
174+
175+
if (token.estree) {
176+
attributes[attributes.length - 1].value.data = {estree: token.estree}
177+
}
178+
}
179+
180+
function exitMdxJsxTagSelfClosingMarker() {
181+
this.getData('mdxJsxTag').selfClosing = true
182+
}
183+
184+
function exitMdxJsxTag(token) {
185+
var tag = this.getData('mdxJsxTag')
186+
var stack = this.getData('mdxJsxTagStack')
187+
var tail = stack[stack.length - 1]
188+
189+
if (tag.close && tail.name !== tag.name) {
190+
throw new VMessage(
191+
'Unexpected closing tag `' +
192+
serializeAbbreviatedTag(tag) +
193+
'`, expected corresponding closing tag for `' +
194+
serializeAbbreviatedTag(tail) +
195+
'` (' +
196+
stringifyPosition(tail) +
197+
')',
198+
{start: token.start, end: token.end},
199+
'mdast-util-mdx-jsx:end-tag-mismatch'
200+
)
201+
}
202+
203+
// End of a tag, so drop the buffer.
204+
this.resume()
205+
206+
if (tag.close) {
207+
stack.pop()
208+
} else {
209+
this.enter(
210+
{
211+
type:
212+
token.type === 'mdxJsxTextTag'
213+
? 'mdxJsxTextElement'
214+
: 'mdxJsxFlowElement',
215+
name: tag.name,
216+
attributes: tag.attributes,
217+
children: []
218+
},
219+
token
220+
)
221+
}
222+
223+
if (tag.selfClosing || tag.close) {
224+
this.exit(token)
225+
} else {
226+
stack.push(tag)
227+
}
228+
}
229+
230+
// Serialize a tag, excluding attributes.
231+
// `self-closing` is not supported, because we don’t need it yet.
232+
function serializeAbbreviatedTag(tag) {
233+
return '<' + (tag.close ? '/' : '') + (tag.name || '') + '>'
234+
}

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
exports.fromMarkdown = require('./from-markdown')
2+
exports.toMarkdown = require('./to-markdown')

license

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(The MIT License)
2+
3+
Copyright (c) 2020 Titus Wormer <tituswormer@gmail.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
'Software'), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package.json

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"name": "mdast-util-mdx-jsx",
3+
"version": "0.0.0",
4+
"description": "mdast extension to parse and serialize MDX or MDX.js JSX",
5+
"license": "MIT",
6+
"keywords": [
7+
"unist",
8+
"mdast",
9+
"mdast-util",
10+
"util",
11+
"utility",
12+
"markdown",
13+
"markup",
14+
"mdx",
15+
"mdxjs",
16+
"jsx",
17+
"extension"
18+
],
19+
"repository": "syntax-tree/mdast-util-mdx-jsx",
20+
"bugs": "https://github.com/syntax-tree/mdast-util-mdx-jsx/issues",
21+
"funding": {
22+
"type": "opencollective",
23+
"url": "https://opencollective.com/unified"
24+
},
25+
"author": "Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)",
26+
"contributors": [
27+
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
28+
],
29+
"files": [
30+
"from-markdown.js",
31+
"index.js",
32+
"to-markdown.js"
33+
],
34+
"dependencies": {
35+
"mdast-util-to-markdown": "^0.5.0",
36+
"parse-entities": "^2.0.0",
37+
"stringify-entities": "^3.1.0",
38+
"unist-util-remove-position": "^3.0.0",
39+
"vfile-message": "^2.0.0"
40+
},
41+
"devDependencies": {
42+
"acorn": "^8.0.0",
43+
"mdast-util-from-markdown": "^0.8.0",
44+
"micromark-extension-mdx-jsx": "^0.1.0",
45+
"nyc": "^15.0.0",
46+
"prettier": "^2.0.0",
47+
"remark-cli": "^9.0.0",
48+
"remark-preset-wooorm": "^8.0.0",
49+
"tape": "^5.0.0",
50+
"xo": "^0.35.0"
51+
},
52+
"scripts": {
53+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
54+
"test-api": "node test",
55+
"test-coverage": "nyc --reporter lcov tape test.js",
56+
"test": "npm run format && npm run test-coverage"
57+
},
58+
"nyc": {
59+
"check-coverage": true,
60+
"lines": 100,
61+
"functions": 100,
62+
"branches": 100
63+
},
64+
"prettier": {
65+
"tabWidth": 2,
66+
"useTabs": false,
67+
"singleQuote": true,
68+
"bracketSpacing": false,
69+
"semi": false,
70+
"trailingComma": "none"
71+
},
72+
"xo": {
73+
"prettier": true,
74+
"esnext": false,
75+
"rules": {
76+
"eqeqeq": "off",
77+
"no-eq-null": "off",
78+
"unicorn/explicit-length-check": "off"
79+
}
80+
},
81+
"remarkConfig": {
82+
"plugins": [
83+
"preset-wooorm"
84+
]
85+
}
86+
}

0 commit comments

Comments
 (0)