Skip to content

Commit 8d362d8

Browse files
committed
Populate list/list item metadata by default
1 parent d57b559 commit 8d362d8

File tree

7 files changed

+60
-31
lines changed

7 files changed

+60
-31
lines changed

demo/dist/js/demo.js

Lines changed: 1 addition & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Type definitions for react-markdown > v3.3.0
22
// Project: https://github.com/rexxars/react-markdown
3-
// Definitions by: Ruslan Ibragimov <https://github.com/IRus>, Kohei Asai <[email protected]>, ClassicDarkChocolate<https://github.com/ClassicDarkChocolate>
3+
// Definitions by:
4+
// - Ruslan Ibragimov <https://github.com/IRus>
5+
// - Kohei Asai <[email protected]>
6+
// - ClassicDarkChocolate <https://github.com/ClassicDarkChocolate>
7+
// - Espen Hovlandsdal <https://espen.codes/>
48

59
import {Component, ReactElement, ReactNode, ReactType} from 'react'
610

@@ -91,6 +95,7 @@ declare namespace ReactMarkdown {
9195
checked?: boolean | null
9296
children?: MarkdownAbstractSyntaxTree[]
9397
data?: {[key: string]: any}
98+
index?: number
9499
depth?: number
95100
height?: number
96101
identifier?: string

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"staticpath": "demo/dist"
3535
},
3636
"dependencies": {
37+
"mdast-add-list-metadata": "^1.0.0",
3738
"prop-types": "^15.6.1",
3839
"remark-parse": "^5.0.0",
3940
"unified": "^6.1.5",

src/react-markdown.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const xtend = require('xtend')
44
const unified = require('unified')
55
const parse = require('remark-parse')
66
const PropTypes = require('prop-types')
7+
const addListMetadata = require('mdast-add-list-metadata')
78
const naiveHtml = require('./plugins/naive-html')
89
const disallowNode = require('./plugins/disallow-node')
910
const astToReact = require('./ast-to-react')
@@ -43,7 +44,7 @@ function applyParserPlugin(parser, plugin) {
4344
}
4445

4546
function determineAstPlugins(props) {
46-
const plugins = [wrapTableRows]
47+
const plugins = [wrapTableRows, addListMetadata()]
4748

4849
let disallowedTypes = props.disallowedTypes
4950
if (props.allowedTypes) {

test/__snapshots__/react-markdown.test.js.snap

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,29 @@ foo();
11491149
</div>
11501150
`;
11511151

1152+
exports[`should pass depth, index and ordered props to list/listItem 1`] = `
1153+
<div>
1154+
<ul>
1155+
<li>
1156+
foo
1157+
<ol
1158+
start="2"
1159+
>
1160+
<li>
1161+
bar
1162+
</li>
1163+
<li>
1164+
baz
1165+
</li>
1166+
</ol>
1167+
</li>
1168+
<li>
1169+
root
1170+
</li>
1171+
</ul>
1172+
</div>
1173+
`;
1174+
11521175
exports[`should pass on raw source position to non-tag renderers if rawSourcePos option is enabled 1`] = `
11531176
Position {
11541177
"end": Object {

test/react-markdown.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ test('should handle ordered lists with a start index', () => {
205205
expect(component.toJSON()).toMatchSnapshot()
206206
})
207207

208+
test('should pass depth, index and ordered props to list/listItem', () => {
209+
const input = '- foo\n 2. bar\n 3. baz\n- root\n'
210+
const renderers = {
211+
listItem: item => {
212+
expect(item.index).toBeGreaterThanOrEqual(0)
213+
expect(item.ordered).not.toBeUndefined()
214+
return Markdown.renderers.listItem(item)
215+
},
216+
list: item => {
217+
expect(item.depth).toBeGreaterThanOrEqual(0)
218+
return Markdown.renderers.list(item)
219+
}
220+
}
221+
const component = renderer.create(<Markdown source={input} renderers={renderers} />)
222+
expect(component.toJSON()).toMatchSnapshot()
223+
})
224+
208225
test('should handle inline html with escapeHtml option enabled', () => {
209226
const input = 'I am having <strong>so</strong> much fun'
210227
const component = renderer.create(<Markdown source={input} />)

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,6 +4226,12 @@ md5.js@^1.3.4:
42264226
hash-base "^3.0.0"
42274227
inherits "^2.0.1"
42284228

4229+
mdast-add-list-metadata@^1.0.0:
4230+
version "1.0.0"
4231+
resolved "https://registry.yarnpkg.com/mdast-add-list-metadata/-/mdast-add-list-metadata-1.0.0.tgz#14821c46e54cdea0017d10bf8a99e1cecbc1bfab"
4232+
dependencies:
4233+
unist-util-visit-parents "1.1.2"
4234+
42294235
mem-fs-editor@^3.0.0:
42304236
version "3.0.2"
42314237
resolved "https://registry.yarnpkg.com/mem-fs-editor/-/mem-fs-editor-3.0.2.tgz#dd0a6eaf2bb8a6b37740067aa549eb530105af9f"
@@ -6400,6 +6406,10 @@ unist-util-stringify-position@^1.0.0, unist-util-stringify-position@^1.1.1:
64006406
version "1.1.1"
64016407
resolved "https://registry.yarnpkg.com/unist-util-stringify-position/-/unist-util-stringify-position-1.1.1.tgz#3ccbdc53679eed6ecf3777dd7f5e3229c1b6aa3c"
64026408

6409+
6410+
version "1.1.2"
6411+
resolved "https://registry.yarnpkg.com/unist-util-visit-parents/-/unist-util-visit-parents-1.1.2.tgz#f6e3afee8bdbf961c0e6f028ea3c0480028c3d06"
6412+
64036413
unist-util-visit@^1.1.0:
64046414
version "1.2.0"
64056415
resolved "https://registry.yarnpkg.com/unist-util-visit/-/unist-util-visit-1.2.0.tgz#9dc78d1f95cd242e865f7f93f327d3296bb9a718"

0 commit comments

Comments
 (0)