Skip to content

Commit

Permalink
Make Youtube link clikable
Browse files Browse the repository at this point in the history
  • Loading branch information
tpmai22 committed Jan 31, 2022
1 parent 0fd339c commit 4b2de81
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
"ioredis": "4.28.2",
"ioredis-mock": "5.8.1",
"jsdom": "18.1.1",
"linkify-html": "^3.0.5",
"linkifyjs": "^3.0.5",
"node-fetch": "2.6.7",
"normalize-url": "6.1.0",
"opml-generator": "1.1.1",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion src/backend/data/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,26 @@ class Post {
article.date = article.pubdate;
}

if (Array.isArray(article.content) && determinePostType(article.link) === 'video') {
article.content = article.content.join(' ');
}

let html;
try {
// The article.content is frequently the full HTML article content.
// Sanitize it of any scripts or other dangerous attributes/elements,
// add lazy loading for <img> and <iframe>, and syntax highlight all
// <pre><code>...</code></pre> blocks.
html = processHTML(article.content);
html = processHTML(article.content, determinePostType(article.link));
} catch (error) {
logger.error({ error }, 'Unable to process HTML for feed');
throw error;
}

// if (determinePostType(article.link) === 'video') {
// html = determinePostType(article.link) === 'video' ? linkifyHtml(html) : html;
// }

// NOTE: feedparser article properties are documented here:
// https://www.npmjs.com/package/feedparser#list-of-article-properties
const post = new Post(
Expand Down
7 changes: 5 additions & 2 deletions src/backend/utils/html/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const jsdom = require('jsdom');
const linkifyHtml = require('linkify-html');
const sanitize = require('./sanitize');
const fixIFrameWidth = require('./fix-iframe-width');
const lazyLoad = require('./lazy-load');
Expand All @@ -14,14 +15,16 @@ const { JSDOM } = jsdom;
* modifies <img> and <iframe> elements to be lazy loaded. Returns
* the updated HTML.
*/
module.exports = function process(html) {
module.exports = function process(html, type) {
// Expect a String, return anything else untouched.
if (typeof html !== 'string') {
return html;
}

const linkifiedHtml = type === 'video' ? linkifyHtml(html) : html;

// Sanitize the HTML to remove unwanted elements and attributes
const clean = sanitize(html);
const clean = sanitize(linkifiedHtml);

// Checks if the context of the sanitized html contains whitespace only.
const fragment = JSDOM.fragment(clean);
Expand Down

0 comments on commit 4b2de81

Please sign in to comment.