From 228d8e8e47e913fc6487a917d43a59070aefa0ab Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 31 Jan 2022 10:07:38 +0100 Subject: Convert markdown to html/plain text for feeds --- server/controllers/feeds.ts | 9 +++++---- server/helpers/markdown.ts | 4 ++-- server/tests/feeds/feeds.ts | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/server/controllers/feeds.ts b/server/controllers/feeds.ts index 29502a154..3c8680ca4 100644 --- a/server/controllers/feeds.ts +++ b/server/controllers/feeds.ts @@ -1,5 +1,6 @@ import express from 'express' import Feed from 'pfeed' +import { mdToPlainText, toSafeHtml } from '@server/helpers/markdown' import { getServerActor } from '@server/models/application/application' import { getCategoryLabel } from '@server/models/video/formatter/video-format-utils' import { VideoInclude } from '@shared/models' @@ -119,7 +120,7 @@ async function generateVideoCommentsFeed (req: express.Request, res: express.Res title, id: comment.url, link, - content: comment.text, + content: toSafeHtml(comment.text), author, date: comment.createdAt }) @@ -235,7 +236,7 @@ function initFeed (parameters: { return new Feed({ title: name, - description, + description: mdToPlainText(description), // updated: TODO: somehowGetLatestUpdate, // optional, default = today id: webserverUrl, link: webserverUrl, @@ -298,8 +299,8 @@ function addVideosToFeed (feed, videos: VideoModel[]) { title: video.name, id: video.url, link: WEBSERVER.URL + video.getWatchStaticPath(), - description: video.getTruncatedDescription(), - content: video.description, + description: mdToPlainText(video.getTruncatedDescription()), + content: toSafeHtml(video.description), author: [ { name: video.VideoChannel.Account.getDisplayName(), diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts index ebf479b98..0b8c2fabc 100644 --- a/server/helpers/markdown.ts +++ b/server/helpers/markdown.ts @@ -10,7 +10,7 @@ const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, markdownIt.enable(TEXT_WITH_HTML_RULES) markdownIt.use(markdownItEmoji) -const toSafeHtml = text => { +const toSafeHtml = (text: string) => { if (!text) return '' // Restore line feed @@ -23,7 +23,7 @@ const toSafeHtml = text => { return sanitizeHtml(html, sanitizeOptions) } -const mdToPlainText = text => { +const mdToPlainText = (text: string) => { if (!text) return '' // Convert possible markdown (emojis, emphasis and lists) to html diff --git a/server/tests/feeds/feeds.ts b/server/tests/feeds/feeds.ts index 24a518342..74cbaeb6d 100644 --- a/server/tests/feeds/feeds.ts +++ b/server/tests/feeds/feeds.ts @@ -274,8 +274,8 @@ describe('Test syndication feeds', () => { const jsonObj = JSON.parse(json) expect(jsonObj.items.length).to.be.equal(2) - expect(jsonObj.items[0].html_content).to.equal('super comment 2') - expect(jsonObj.items[1].html_content).to.equal('super comment 1') + expect(jsonObj.items[0].html_content).to.contain('

super comment 2

') + expect(jsonObj.items[1].html_content).to.contain('

super comment 1

') } }) -- cgit v1.2.3