-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add image component caption as children (#918)
[![PR App][icn]][demo] | Fix RM-10049 :-------------------:|:----------: ## 🧰 Changes cleaner way to add/update image caption ## 🧬 QA & Testing - [Broken on production][prod]. - [Working in this PR app][demo]. [demo]: https://markdown-pr-PR_NUMBER.herokuapp.com [prod]: https://SUBDOMAIN.readme.io [icn]: https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
- Loading branch information
1 parent
e680bd8
commit 59e1bd3
Showing
7 changed files
with
95 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
import { mdast } from '../../index'; | ||
|
||
describe('embeds transformer', () => { | ||
it('converts a link with a title of "@embed" to an embed-block', () => { | ||
const md = ` | ||
[alt](https://example.com/cool.pdf "@embed") | ||
`; | ||
const tree = mdast(md); | ||
|
||
expect(tree.children[0].type).toBe('embed-block'); | ||
expect(tree.children[0].data.hProperties.title).toBe('alt'); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { mdast } from '../../index'; | ||
|
||
describe('images transformer', () => { | ||
it('converts single children images of paragraphs to an image-block', () => { | ||
const md = ` | ||
 | ||
`; | ||
const tree = mdast(md); | ||
|
||
expect(tree.children[0].type).toBe('image-block'); | ||
expect(tree.children[0].data.hProperties.src).toBe('https://example.com/image.jpg'); | ||
}); | ||
|
||
it('can parse the caption markdown to children', () => { | ||
const md = ` | ||
<Image src="https://example.com/image.jpg" caption="**this** is *markdown*" /> | ||
`; | ||
const tree = mdast(md); | ||
|
||
expect(tree.children[0].children[0].children[0].type).toBe('strong'); | ||
expect(tree.children[0].children[0].children[2].type).toBe('emphasis'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
import { visit } from 'unist-util-visit'; | ||
import { Node, Paragraph, Parents } from 'mdast'; | ||
import { MdxJsxFlowElement } from 'mdast-util-mdx'; | ||
|
||
import { NodeTypes } from '../../enums'; | ||
import { ImageBlock } from '../../types'; | ||
import { getAttrs } from '../utils'; | ||
import { mdast } from '../../lib'; | ||
|
||
const imageTransformer = () => { | ||
return (tree: Node) => { | ||
visit(tree, 'paragraph', (node: Paragraph, i: number, parent: Parents) => { | ||
// check if inline or already transformed | ||
if (parent.type !== 'root' || node.children?.length > 1 || node.children[0].type !== 'image') return; | ||
const [{ alt, url, title }] = node.children as any; | ||
|
||
const newNode = { | ||
type: NodeTypes.imageBlock, | ||
alt, | ||
title, | ||
url, | ||
data: { | ||
hName: 'img', | ||
hProperties: { | ||
...(alt && { alt }), | ||
src: url, | ||
...(title && { title }), | ||
}, | ||
const imageTransformer = () => (tree: Node) => { | ||
visit(tree, 'paragraph', (node: Paragraph, i: number, parent: Parents) => { | ||
// check if inline | ||
if ( | ||
parent.type !== 'root' || | ||
node.children?.length > 1 || | ||
node.children[0].type !== 'image' | ||
) | ||
return; | ||
|
||
const [{ alt, url, title }] = node.children as any; | ||
|
||
const newNode = { | ||
type: NodeTypes.imageBlock, | ||
alt, | ||
title, | ||
url, | ||
children: [], | ||
data: { | ||
hName: 'img', | ||
hProperties: { | ||
...(alt && { alt }), | ||
src: url, | ||
...(title && { title }), | ||
}, | ||
position: node.position, | ||
} as ImageBlock; | ||
}, | ||
position: node.position, | ||
} as ImageBlock; | ||
|
||
parent.children.splice(i, 1, newNode); | ||
}); | ||
|
||
const isImage = (node: MdxJsxFlowElement) => node.name === 'Image'; | ||
|
||
visit(tree, isImage, (node: MdxJsxFlowElement) => { | ||
const attrs = getAttrs<ImageBlock['data']['hProperties']>(node); | ||
|
||
if (attrs.caption) { | ||
node.children = mdast(attrs.caption).children; | ||
} | ||
|
||
}); | ||
|
||
parent.children.splice(i, 1, newNode); | ||
}); | ||
}; | ||
return tree; | ||
}; | ||
|
||
export default imageTransformer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters