diff --git a/lib/routes/infoq/utils.js b/lib/routes/infoq/utils.js index 0b860588e2..bcffc104e6 100644 --- a/lib/routes/infoq/utils.js +++ b/lib/routes/infoq/utils.js @@ -26,7 +26,7 @@ const ProcessFeed = async (list, cache) => { return { title: data.article_title, - description: data.content, + description: parseContent(data.content), pubDate, author: author, link, @@ -40,6 +40,60 @@ const ProcessFeed = async (list, cache) => { return items; }; +const parseToSimpleText = (content) => parseToSimpleTexts(content).join(''); +const parseToSimpleTexts = (content) => + content.map((i) => { + const funcMaps = { + doc: () => + parseToSimpleTexts(i.content) + .map((v) => `
${v}
`) + .join(''), + text: () => i.text, + heading: () => { + const level = i.attrs.level; + const text = parseToSimpleText(i.content); + return `${text}`; + }, + image: () => { + const img = i.attrs.src; + return `
${code}`;
+ },
+ link: () => {
+ const href = i.attrs.href;
+ const text = parseToSimpleText(i.content);
+ return `${text}"`;
+ },
+ };
+
+ if (i.type in funcMaps) {
+ return funcMaps[i.type]();
+ }
+
+ if (!i.content) {
+ return '';
+ }
+
+ return parseToSimpleText(i.content);
+ });
+
+function parseContent(content) {
+ const isRichContent = content.startsWith(`{"`);
+ if (!isRichContent) {
+ return content;
+ }
+
+ return parseToSimpleText([JSON.parse(content)]);
+}
+
module.exports = {
ProcessFeed,
};