-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from HiDeoo/hd-fix-rss-images
- Loading branch information
Showing
4 changed files
with
33 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,33 @@ | ||
import { Marked } from 'marked' | ||
import markedPlaintify from 'marked-plaintify' | ||
import { transform } from 'ultrahtml' | ||
import { html, transform } from 'ultrahtml' | ||
import sanitize from 'ultrahtml/transformers/sanitize' | ||
import swap from 'ultrahtml/transformers/swap' | ||
|
||
const markedMd = new Marked({ gfm: true }) | ||
const markedText = new Marked({ gfm: true }, markedPlaintify()) | ||
|
||
const imagePlaceholder = | ||
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA0MDAgMjUwIiB3aWR0aD0iNDAwIiBoZWlnaHQ9IjI1MCI+CiAgPHJlY3Qgd2lkdGg9IjQwMCIgaGVpZ2h0PSIyNTAiIGZpbGw9IiNCM0IwQjBGRiI+PC9yZWN0PgogIDx0ZXh0IHg9IjUwJSIgeT0iNTAlIiBkb21pbmFudC1iYXNlbGluZT0ibWlkZGxlIiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBmb250LWZhbWlseT0ibW9ub3NwYWNlIiBmb250LXNpemU9IjI1cHgiIGZpbGw9IiMyNjI2MjZGRiI+SW1hZ2U8L3RleHQ+ICAgCjwvc3ZnPg==' | ||
|
||
export async function stripMarkdown(markdown: string) { | ||
return await markedText.parse(markdown) | ||
} | ||
|
||
export async function renderMarkdownToHTML(markdown: string) { | ||
export async function renderMarkdownToHTML(markdown: string, link: URL) { | ||
const content = await markedMd.parse(markdown) | ||
|
||
return transform(content, [sanitize()]) | ||
return transform(content, [ | ||
swap({ | ||
// Strip images from the content and replace them with a placeholder. | ||
// Re-evaluate this when the Container API is available in Astro. | ||
img: (props) => | ||
html`<a href="${link}"> | ||
<div><img src="${imagePlaceholder}" alt="${props['alt']}" /></div> | ||
<blockquote><em>Original image available in the blog post.</em></blockquote> | ||
</a> | ||
<br />`, | ||
}), | ||
sanitize(), | ||
]) | ||
} |
This file contains 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