Posts now use displayable_description
This commit is contained in:
parent
0788c78711
commit
20008d7c2d
|
@ -41,9 +41,15 @@ export interface MeiliVideo extends VideoAttributesFragment {
|
|||
channel_uid?: string;
|
||||
}
|
||||
|
||||
export interface MeiliPost extends PostAttributesFragment {
|
||||
export interface MeiliPost extends Omit<PostAttributesFragment, "translations"> {
|
||||
id: string;
|
||||
sortable_date: number;
|
||||
translations: (Omit<
|
||||
NonNullable<NonNullable<PostAttributesFragment["translations"]>[number]>,
|
||||
"body"
|
||||
> & {
|
||||
displayable_description?: string | null;
|
||||
})[];
|
||||
}
|
||||
|
||||
export interface MeiliWikiPage extends Omit<WikiPageAttributesFragment, "translations"> {
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
import { convert } from "html-to-text";
|
||||
import { marked } from "marked";
|
||||
import { isDefinedAndNotEmpty } from "./asserts";
|
||||
import DOMPurify from "isomorphic-dompurify";
|
||||
|
||||
export const prettySlug = (slug?: string, parentSlug?: string): string => {
|
||||
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 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();
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ import {
|
|||
isDefinedAndNotEmpty,
|
||||
} from "core/helpers/asserts";
|
||||
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 { MeiliSearch } from "meilisearch";
|
||||
|
||||
|
@ -47,9 +47,9 @@ const transformContent: TransformFunction<MeiliIndices.CONTENT> = (data) => {
|
|||
translations: filterDefined(translations).map(
|
||||
({ text_set, description, ...otherTranslatedFields }) => {
|
||||
let displayable_description = "";
|
||||
if (isDefinedAndNotEmpty(description)) displayable_description += description;
|
||||
if (isDefinedAndNotEmpty(text_set?.text))
|
||||
displayable_description += `\n\n${text_set?.text}`;
|
||||
if (isDefinedAndNotEmpty(description))
|
||||
displayable_description += prettyMarkdown(description);
|
||||
if (text_set?.text) displayable_description += `\n\n${prettyMarkdown(text_set.text)}`;
|
||||
return {
|
||||
...otherTranslatedFields,
|
||||
displayable_description,
|
||||
|
@ -88,11 +88,24 @@ const transformPost: TransformFunction<MeiliIndices.POST> = (data) => {
|
|||
if (!data) throw new Error(`Data is empty ${MeiliIndices.POST}`);
|
||||
if (!data.attributes || !data.id)
|
||||
throw new Error(`Incorrect data stucture on ${MeiliIndices.POST}`);
|
||||
const { id, attributes } = data;
|
||||
const {
|
||||
id,
|
||||
attributes: { translations, ...otherAttributes },
|
||||
} = data;
|
||||
return {
|
||||
id,
|
||||
...attributes,
|
||||
sortable_date: getUnixTime(datePickerToDate(attributes.date)),
|
||||
sortable_date: getUnixTime(datePickerToDate(otherAttributes.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,
|
||||
};
|
||||
};
|
||||
|
||||
|
|
|
@ -60,7 +60,7 @@ export const synchronizeStrapiAndMeili = async (): Promise<void> => {
|
|||
processIndex(
|
||||
MeiliIndices.POST,
|
||||
posts.posts?.data.map((item) => strapiToMeiliTransformFunctions.post(item)),
|
||||
["translations.title", "translations.excerpt", "translations.body"],
|
||||
["translations.title", "translations.displayable_description"],
|
||||
["sortable_date"],
|
||||
["hidden"]
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue