feat: infoq richContent simple render (#4684)

This commit is contained in:
Mitt
2020-05-07 00:38:06 +08:00
committed by GitHub
parent 0050f8882f
commit 0b4edcd351

View File

@@ -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) => `<p>${v}</p>`)
.join(''),
text: () => i.text,
heading: () => {
const level = i.attrs.level;
const text = parseToSimpleText(i.content);
return `<h${level}>${text}</h${level}>`;
},
blockquote: () => {
const text = parseToSimpleText(i.content);
return `<blockquote>${text}</blockquote>`;
},
image: () => {
const img = i.attrs.src;
return `<img src="${img}"></img>`;
},
codeblock: () => {
const lang = i.attrs.lang;
const code = parseToSimpleText(i.content);
return `<code lang="${lang}">${code}</code>`;
},
link: () => {
const href = i.attrs.href;
const text = parseToSimpleText(i.content);
return `<a href="${href}">${text}</a>"`;
},
};
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,
};