-
Notifications
You must be signed in to change notification settings - Fork 11
fix: embeds and images save better #893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
3f7f9c1
5371d1d
ef1c17b
394ebbc
d4b0ff2
048716f
fe91aea
358741b
ab7f718
89afef1
ec3f201
c3442d8
b00921a
fa0a549
05c182b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
coverage | ||
node_modules | ||
|
||
.env | ||
.idea/ | ||
.vscode/ | ||
*.code-* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { mdast } from '../index'; | ||
|
||
describe.skip('Parse html block', () => { | ||
describe('Parse html block', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚀 |
||
it('parses an html block', () => { | ||
const text = ` | ||
<div>Some block html</div> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,10 @@ | ||
import type { Embed } from "types"; | ||
|
||
const embed = (node: Embed) => { | ||
const { image, favicon, iframe, title, url } = node.data?.hProperties || {}; | ||
const complexEmbed: boolean = Boolean(image) || Boolean(favicon) || iframe; | ||
|
||
if (complexEmbed) { | ||
const attributes = Object.keys(node.data?.hProperties).map(key => `${key}="${node.data?.hProperties[key]}"`).join(' ') | ||
// TODO: make this a util | ||
return `<Embed ${attributes} />`; | ||
} | ||
|
||
return `[${title}](${url} "@embed")'`; | ||
// TODO: make this a util | ||
const attributes = Object.keys(node.data?.hProperties).map(key => `${key}='${node.data?.hProperties[key]}'`).join(' ') | ||
|
||
return `<Embed ${attributes} />`; | ||
} | ||
|
||
export default embed; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import type { Image } from 'mdast'; | ||
|
||
const image = (node: Image) => { | ||
const { align, className, width } = node.data?.hProperties || {}; | ||
const complexImage: boolean = Boolean(width) || Boolean(className) || Boolean(align); | ||
|
||
const { align, border, width, src } = node.data?.hProperties || {}; | ||
const complexImage: boolean = Boolean(width) || Boolean(border) || Boolean(align); | ||
if (complexImage) { | ||
const attributes = Object.keys(node.data?.hProperties) | ||
.map(key => `${key}="${node.data?.hProperties[key]}"`) | ||
.join(' '); | ||
return `<Image ${attributes} />`; | ||
} | ||
|
||
return `` : ')'}`; | ||
}; | ||
return `` : ')'})`; | ||
jennspencer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export default image; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { visit } from 'unist-util-visit'; | ||
|
||
import { NodeTypes } from '../../enums'; | ||
|
||
const imageTransformer = () => { | ||
return (tree: any) => { | ||
visit(tree, 'image', (node, _, parent) => { | ||
// check if inline or already transformed | ||
if (parent.type !== 'paragraph' || parent.children.length > 1 || node.data?.hName === 'image') return; | ||
const newNode = { | ||
type: NodeTypes.image, | ||
data: { | ||
hProperties: { | ||
title: node.title, | ||
src: node.url, | ||
alt: node.alt, | ||
align: node.align, | ||
border: node.border, | ||
width: node.width, | ||
caption: node.caption, | ||
lazy: node.lazy, | ||
}, | ||
hName: 'image', | ||
}, | ||
position: node.position, | ||
}; | ||
parent = newNode; | ||
jennspencer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}; | ||
}; | ||
|
||
export default imageTransformer; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import calloutTransformer from './callouts'; | ||
import codeTabsTransfromer from './code-tabs'; | ||
import embedTransformer from './embeds'; | ||
import imageTransformer from './images'; | ||
import gemojiTransformer from './gemoji+'; | ||
import readmeComponentsTransformer from './readme-components'; | ||
|
||
export { readmeComponentsTransformer }; | ||
|
||
export default [calloutTransformer, codeTabsTransfromer, embedTransformer, gemojiTransformer]; | ||
export default [calloutTransformer, codeTabsTransfromer, embedTransformer, imageTransformer, gemojiTransformer]; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -52,19 +52,19 @@ const coerceJsxToMd = | |||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
parent.children[index] = mdNode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (node.name === 'Image') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { position } = node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { alt = '', url, title = null } = attributes<Pick<Image, 'alt' | 'title' | 'url'>>(node); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
const mdNode: Image = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
alt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
position, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
title, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
type: 'image', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
parent.children[index] = mdNode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// } else if (node.name === 'Image') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// const { position } = node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// const { alt = '', url, title = null } = attributes<Pick<Image, 'alt' | 'title' | 'url'>>(node); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// const mdNode: Image = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// alt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// position, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// title, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// type: 'image', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// url, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
// }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
// parent.children[index] = mdNode; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kellyjosephprice i commented out some of your image stuff because it was gettin' rull weird with my changes and i figured we could work it out later There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this means 'complexImages' won't render as the image widget in the editor, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same for embeds, I think we need to convert them from JSX to mdast/slate-y type nodes for the editor to pick them up. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it works in the editor playground, but i haven't checked in a project yet (doing so now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you change the playground source to:
does it load as a widget or as JSX? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OH I think it's falling through to here: markdown/processor/transform/readme-components.ts Lines 97 to 111 in ef1c17b
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add embeds to this map: markdown/processor/transform/readme-components.ts Lines 9 to 19 in ef1c17b
And then they'll get picked up in the editor? |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
} else if (node.name === 'Table') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { children, position } = node; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
const { align = [...new Array(node.children.length)].map(() => null) } = attributes<Pick<Table, 'align'>>(node); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hee hee