Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 1 addition & 64 deletions packages/markdown/components/Embed.jsx
Original file line number Diff line number Diff line change
@@ -1,74 +1,11 @@
const React = require('react');
const propTypes = require('prop-types');

// const Embedly = require('embedly');
// const api =
// process.env.NODE_ENV !== 'test'
// ? new Embedly({ key: '' })
// : {
// extract: (opts, cb) => {
// cb(false, [{ media: { html: 'testing' } }]);
// },
// };

class Embed extends React.Component {
constructor(props) {
super(props);
this.state = {
embedly: false,
};
}

// componentDidMount() {
// this.getEmbed();
// }

/* istanbul ignore next */
// getGist(data) {
// const [, gistID] = this.props.url.match(
// /(?:gist.github.com\/(?:.[-_a-zA-Z0-9]+\/)?([-_a-zA-Z0-9]*)(?:\.git|\.js)?)/,
// );
// fetch(`https://api.github.com/gists/${gistID}`)
// .then(r => r.json())
// .then(gist => {
// const files = gist.files;
// const keys = Object.keys(files);
// const file = files[keys[0]];
// data.media.html = `<pre><code>${file.content}</code></pre>`;
// this.setState({ embedly: data });
// });
// }

/* istanbul ignore next */
// getEmbed() {
// api.extract({ url: this.props.url }, (err, obj) => {
// if (err) {
// // eslint-disable-next-line no-console
// console.error(err);
// return err;
// }
// const result = obj[0];
// if (result.provider_display === 'gist.github.com') return this.getGist(result);
// return this.setState({ embedly: result });
// });
// }

render() {
return (
<div className="embed">
<div
className="embed-media"
dangerouslySetInnerHTML={{ __html: this.props.html }}
// dangerouslySetInnerHTML={
// (typeof this.state.embedly === 'object' &&
// 'media' in this.state.embedly &&
// 'html' in this.state.embedly.media && { __html: this.state.embedly.media.html }) || {
// __html: this.state.embedly.error_message,
// } || {
// __html: 'Loading...',
// }
// }
></div>
<div className="embed-media" dangerouslySetInnerHTML={{ __html: this.props.html }}></div>
</div>
);
}
Expand Down
64 changes: 32 additions & 32 deletions packages/markdown/processor/parse/magic-block-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function tokenize(eat, value) {
type = type.trim();
json = (json && JSON.parse(json)) || {};

if (Object.keys(json).length < 1) return eat(match);

switch (type) {
case 'code': {
const children = json.codes.map(obj => ({
Expand Down Expand Up @@ -57,40 +59,40 @@ function tokenize(eat, value) {
{
type: 'heading',
depth: json.level || 2,
children: this.tokenizeInline(json.title, eat.now()),
children: 'title' in json ? this.tokenizeInline(json.title, eat.now()) : '',
},
json
)
);
}
case 'image': {
return eat(match)(
WrapPinnedBlocks(
json.images.map(img => {
const [url, title] = img.image;
return {
type: 'image',
url,
title,
alt: img.caption,
data: {
hProperties: {
caption: img.caption,
},
},
};
})[0],
json
)
);
const imgs = json.images.map(img => {
const [url, title] = img.image;
return {
type: 'image',
url,
title,
alt: img.caption,
data: {
hProperties: {
caption: img.caption,
},
},
};
});
const img = imgs[0];

if (!img.url) return eat(match);
return eat(match)(WrapPinnedBlocks(img, json));
}
case 'callout': {
json.type = {
const types = {
info: ['ℹ', 'info'],
success: ['👍', 'okay'],
warning: ['⚠️', 'warn'],
danger: ['❗️', 'error'],
}[json.type];
};
json.type = json.type in types ? types[json.type] : [json.icon || '👍', json.type];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to handle cases where people have emoji in their docs that we don't recognize as types?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kind of, but it’s really more to account for cases in which users have hardcoded an item option in the magic block's JSON which doesn't correspond to one of our pre-approved themes. (Found this edge case in the Branch docs.)

const [icon, theme] = json.type;
return eat(match)(
WrapPinnedBlocks(
Expand Down Expand Up @@ -133,18 +135,16 @@ function tokenize(eat, value) {
type: row ? 'tableCell' : 'tableHead',
children: this.tokenizeInline(val, eat.now()),
};
// convert falsey values to empty strings
sum[row].children = [...sum[row].children].map(v => v || '');
return sum;
}, []);
return eat(match)(
WrapPinnedBlocks(
{
type: 'table',
align: 'align' in json ? json.align : new Array(json.cols).fill('left'),
children,
},
json
)
);
const table = {
type: 'table',
align: 'align' in json ? json.align : new Array(json.cols).fill('left'),
children: children.filter(v => v || false),
};
return eat(match)(WrapPinnedBlocks(table, json));
}
case 'embed': {
json.title = json.title || 'Embed';
Expand Down