Skip to content

Instantly share code, notes, and snippets.

@mjackson
Created May 7, 2020 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjackson/cc35cda6cfba2e9a0d48236ebe665816 to your computer and use it in GitHub Desktop.
Save mjackson/cc35cda6cfba2e9a0d48236ebe665816 to your computer and use it in GitHub Desktop.
let feed = {
type: 'rss',
props: {
'xmlns:blogChannel': 'https://...',
version: '2.0',
children: [
{
type: 'channel',
props: {
title: 'Remix Blog',
link: 'https://blog.remix.run',
description: '...',
language: 'en-us',
copyright: '...',
children: [
{
type: 'item',
props: {
description: '...',
pubDate: '...',
guid: '...',
dangerouslySetInnerHTML: '<a href="...">click me</a>'
}
}
]
}
}
]
}
};
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
}
function stringifyAttributes(attrs) {
return Object.keys(attrs)
.map(key => `${key}="${escapeHtml(attrs[key])}"`)
.join(' ');
}
function createMarkup({ type, props }) {
let markup = `<${type}`;
let { children, dangerouslySetInnerHTML, ...rest } = props;
if (Object.keys(rest).length > 0) {
markup += ` ${stringifyAttributes(rest)}`;
}
if (dangerouslySetInnerHTML) {
markup += '>' + dangerouslySetInnerHTML + `</${type}>`;
} else if (children) {
let innerMarkup = children.map(createMarkup);
if (innerMarkup === '') {
markup += ` />`;
} else {
markup += `>` + innerMarkup + `</${type}>`;
}
} else {
markup += ` />`;
}
return markup;
}
console.log(createMarkup(feed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment