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 all 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Blank Magic Blocks 1`] = `
Object {
"children": Array [],
"type": "root",
}
`;

exports[`Parse Magic Blocks Callout Blocks 1`] = `
Object {
"children": Array [
Expand Down Expand Up @@ -99,6 +106,75 @@ alert('test');",
},
"type": "code-tabs",
},
Object {
"children": Array [
Object {
"className": "tab-panel",
"data": Object {
"hName": "code",
"hProperties": Object {
"lang": "javascript",
"meta": null,
},
},
"lang": "javascript",
"meta": null,
"type": "code",
"value": "$http.post('/someUrl', data).success(successCallback);

alert('test');",
},
],
"data": Object {
"className": "pin",
"hName": "rdme-pin",
},
"type": "rdme-pin",
},
],
"type": "root",
}
`;

exports[`Parse Magic Blocks Custom Callout Blocks 1`] = `
Object {
"children": Array [
Object {
"children": Array [
Object {
"children": Array [
Object {
"type": "text",
"value": "✨ ",
},
Object {
"type": "text",
"value": "Callout Title",
},
],
"type": "paragraph",
},
Object {
"children": Array [
Object {
"type": "text",
"value": "Lorem ipsum dolor sit amet...",
},
],
"type": "paragraph",
},
],
"data": Object {
"hName": "rdme-callout",
"hProperties": Object {
"icon": "✨",
"theme": "custom_theme",
"title": "Callout Title",
"value": "Lorem ipsum dolor sit amet...",
},
},
"type": "rdme-callout",
},
],
"type": "root",
}
Expand Down Expand Up @@ -159,6 +235,14 @@ Object {
"depth": 2,
"type": "heading",
},
Object {
"lang": null,
"meta": null,
"type": "code",
"value": "[block:api-header]
{}
[/block]",
},
],
"type": "root",
}
Expand Down
33 changes: 33 additions & 0 deletions packages/markdown/__tests__/magic-block-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const process = (text, opts = options) =>
.use(rehypeSanitize)
.parse(text);

test('Blank Magic Blocks', () => {
const text = `[block:api-header]
[/block]`;
expect(process(text)).toMatchSnapshot();
});

test('Sanitize Schema', () => {
parser.sanitize(sanitize);
expect(sanitize).toMatchSnapshot();
Expand All @@ -27,6 +33,10 @@ describe('Parse Magic Blocks', () => {
"title": "MagicBlock Conversion",
"level": 2
}
[/block]

[block:api-header]
{}
[/block]`;
expect(process(text)).toMatchSnapshot();
});
Expand Down Expand Up @@ -68,6 +78,17 @@ describe('Parse Magic Blocks', () => {
}
]
}

[/block][block:code]
{
"sidebar": true,
"codes": [
{
"code": "$http.post('/someUrl', data).success(successCallback);\\n\\nalert('test');",
"language": "javascript"
}
]
}
[/block]`;
expect(process(text)).toMatchSnapshot();
});
Expand Down Expand Up @@ -120,6 +141,18 @@ describe('Parse Magic Blocks', () => {
expect(process(text)).toMatchSnapshot();
});

it('Custom Callout Blocks', () => {
const text = `[block:callout]
{
"type": "custom_theme",
"icon": "✨",
"title": "Callout Title",
"body": "Lorem ipsum dolor sit amet..."
}
[/block]`;
expect(process(text)).toMatchSnapshot();
});

it('Unrecognized Blocks', () => {
const text = `[block:unrecognized]
{
Expand Down
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
72 changes: 39 additions & 33 deletions packages/markdown/processor/parse/magic-block-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ function tokenize(eat, value) {

match = match.trim();
type = type.trim();
json = (json && JSON.parse(json)) || {};
try {
json = JSON.parse(json);
} catch (err) {
json = {};
// eslint-disable-next-line no-console
console.error('Invalid Magic Block JSON:', err);
}

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

switch (type) {
case 'code': {
Expand Down Expand Up @@ -57,40 +65,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
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
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 +141,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
1 change: 1 addition & 0 deletions packages/markdown/processor/parse/variable-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function tokenizeVariable(eat, value, silent) {

if (!match) return false;

/* istanbul ignore if */
if (silent) return true;

// Escaped variables should just return the text
Expand Down