Posts now use displayable_description

This commit is contained in:
DrMint 2023-05-19 14:48:14 +02:00
parent 0788c78711
commit 20008d7c2d
4 changed files with 71 additions and 9 deletions

View File

@ -41,9 +41,15 @@ export interface MeiliVideo extends VideoAttributesFragment {
channel_uid?: string; channel_uid?: string;
} }
export interface MeiliPost extends PostAttributesFragment { export interface MeiliPost extends Omit<PostAttributesFragment, "translations"> {
id: string; id: string;
sortable_date: number; sortable_date: number;
translations: (Omit<
NonNullable<NonNullable<PostAttributesFragment["translations"]>[number]>,
"body"
> & {
displayable_description?: string | null;
})[];
} }
export interface MeiliWikiPage extends Omit<WikiPageAttributesFragment, "translations"> { export interface MeiliWikiPage extends Omit<WikiPageAttributesFragment, "translations"> {

View File

@ -1,4 +1,7 @@
import { convert } from "html-to-text";
import { marked } from "marked";
import { isDefinedAndNotEmpty } from "./asserts"; import { isDefinedAndNotEmpty } from "./asserts";
import DOMPurify from "isomorphic-dompurify";
export const prettySlug = (slug?: string, parentSlug?: string): string => { export const prettySlug = (slug?: string, parentSlug?: string): string => {
let newSlug = slug; let newSlug = slug;
@ -218,3 +221,43 @@ export const slugify = (string: string | undefined): string => {
}; };
export const sJoin = (...args: (string | null | undefined)[]): string => args.join(""); export const sJoin = (...args: (string | null | undefined)[]): string => args.join("");
export const prettyMarkdown = (markdown: string): string => {
const block = (text: string) => `${text}\n\n`;
const escapeBlock = (text: string) => `${escape(text)}\n\n`;
const line = (text: string) => `${text}\n`;
const inline = (text: string) => text;
const newline = () => "\n";
const empty = () => "";
const TxtRenderer: marked.Renderer = {
// Block elements
code: escapeBlock,
blockquote: block,
html: empty,
heading: block,
hr: newline,
list: (text) => block(text.trim()),
listitem: line,
checkbox: empty,
paragraph: block,
table: (header, body) => line(header + body),
tablerow: (text) => line(text.trim()),
tablecell: (text) => `${text} `,
// Inline elements
strong: inline,
em: inline,
codespan: inline,
br: newline,
del: inline,
link: (_0, _1, text) => text,
image: (_0, _1, text) => text,
text: inline,
// etc.
options: {},
};
return convert(
DOMPurify.sanitize(marked(markdown, { renderer: TxtRenderer, mangle: false, headerIds: false }))
).trim();
};

View File

@ -6,7 +6,7 @@ import {
isDefinedAndNotEmpty, isDefinedAndNotEmpty,
} from "core/helpers/asserts"; } from "core/helpers/asserts";
import { datePickerToDate, getUnixTime } from "core/helpers/date"; import { datePickerToDate, getUnixTime } from "core/helpers/date";
import { prettyInlineTitle } from "core/helpers/formatters"; import { prettyInlineTitle, prettyMarkdown } from "core/helpers/formatters";
import { isUntangibleGroupItem } from "core/helpers/libraryItem"; import { isUntangibleGroupItem } from "core/helpers/libraryItem";
import { MeiliSearch } from "meilisearch"; import { MeiliSearch } from "meilisearch";
@ -47,9 +47,9 @@ const transformContent: TransformFunction<MeiliIndices.CONTENT> = (data) => {
translations: filterDefined(translations).map( translations: filterDefined(translations).map(
({ text_set, description, ...otherTranslatedFields }) => { ({ text_set, description, ...otherTranslatedFields }) => {
let displayable_description = ""; let displayable_description = "";
if (isDefinedAndNotEmpty(description)) displayable_description += description; if (isDefinedAndNotEmpty(description))
if (isDefinedAndNotEmpty(text_set?.text)) displayable_description += prettyMarkdown(description);
displayable_description += `\n\n${text_set?.text}`; if (text_set?.text) displayable_description += `\n\n${prettyMarkdown(text_set.text)}`;
return { return {
...otherTranslatedFields, ...otherTranslatedFields,
displayable_description, displayable_description,
@ -88,11 +88,24 @@ const transformPost: TransformFunction<MeiliIndices.POST> = (data) => {
if (!data) throw new Error(`Data is empty ${MeiliIndices.POST}`); if (!data) throw new Error(`Data is empty ${MeiliIndices.POST}`);
if (!data.attributes || !data.id) if (!data.attributes || !data.id)
throw new Error(`Incorrect data stucture on ${MeiliIndices.POST}`); throw new Error(`Incorrect data stucture on ${MeiliIndices.POST}`);
const { id, attributes } = data; const {
id,
attributes: { translations, ...otherAttributes },
} = data;
return { return {
id, id,
...attributes, sortable_date: getUnixTime(datePickerToDate(otherAttributes.date)),
sortable_date: getUnixTime(datePickerToDate(attributes.date)), translations: filterDefined(translations).map(({ body, excerpt, ...otherTranslatedFields }) => {
let displayable_description = "";
if (isDefinedAndNotEmpty(excerpt)) displayable_description += prettyMarkdown(excerpt);
if (body) displayable_description += `\n\n${prettyMarkdown(body)}`;
return {
...otherTranslatedFields,
excerpt,
displayable_description,
};
}),
...otherAttributes,
}; };
}; };

View File

@ -60,7 +60,7 @@ export const synchronizeStrapiAndMeili = async (): Promise<void> => {
processIndex( processIndex(
MeiliIndices.POST, MeiliIndices.POST,
posts.posts?.data.map((item) => strapiToMeiliTransformFunctions.post(item)), posts.posts?.data.map((item) => strapiToMeiliTransformFunctions.post(item)),
["translations.title", "translations.excerpt", "translations.body"], ["translations.title", "translations.displayable_description"],
["sortable_date"], ["sortable_date"],
["hidden"] ["hidden"]
); );