Created
May 7, 2020 23:04
-
-
Save mjackson/cc35cda6cfba2e9a0d48236ebe665816 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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, '&') | |
.replace(/</g, '<') | |
.replace(/>/g, '>') | |
.replace(/"/g, '"') | |
.replace(/'/g, '''); | |
} | |
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