Skip to content

Commit

Permalink
escape chars for xml
Browse files Browse the repository at this point in the history
  • Loading branch information
brycedorn committed Feb 3, 2024
1 parent a8905bb commit b3ddb44
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,25 @@ export function renderRobotsTxt() {
return `User-agent: *\nSitemap: ${BLOG_URL}/sitemap.xml`
}

function escapeXML(s: string) {
return s.replace(/[<>&"']/g, function (c) {
switch (c) {
case '<':
return '&lt;'
case '>':
return '&gt;'
case '&':
return '&amp;'
case '"':
return '&quot;'
case '\'':
return '&apos;'
default:
return c
}
})
}

function renderStructuredData(post?: PostDetailType) {
if (!post) {
return ''
Expand All @@ -93,7 +112,7 @@ function renderStructuredData(post?: PostDetailType) {
const json = {
'@context': 'https://schema.org',
'@type': 'Article',
headline: post.title,
headline: escapeXML(post.title),
image: [post.cover_image],
datePublished: post.published_at,
dateModified: post.edited_at || post.created_at,
Expand Down

0 comments on commit b3ddb44

Please sign in to comment.