Post now use displayable_description

This commit is contained in:
DrMint 2023-05-19 14:47:44 +02:00
parent 22f7c39dff
commit d5e7d704bf
5 changed files with 62 additions and 60 deletions

View File

@ -107,12 +107,8 @@ export const SearchPopup = (): JSX.Element => {
q,
limit: SEARCH_LIMIT,
attributesToRetrieve: ["translations", "thumbnail", "slug", "date", "categories"],
attributesToHighlight: [
"translations.title",
"translations.excerpt",
"translations.body",
],
attributesToCrop: ["translations.body"],
attributesToHighlight: ["translations.title", "translations.displayable_description"],
attributesToCrop: ["translations.displayable_description"],
filter: ["hidden = false"],
},
{
@ -376,12 +372,10 @@ export const SearchPopup = (): JSX.Element => {
onClick={() => setSearchOpened(false)}
translations={filterHasAttributes(item._formatted.translations, [
"language.data.attributes.code",
]).map(({ excerpt, body, language, ...otherAttributes }) => ({
]).map(({ excerpt, displayable_description, language, ...otherAttributes }) => ({
...otherAttributes,
description: containsHighlight(excerpt)
? excerpt
: containsHighlight(body)
? body
description: containsHighlight(displayable_description)
? displayable_description
: excerpt,
language: language.data.attributes.code,
}))}

View File

@ -1,7 +1,5 @@
import { convert } from "html-to-text";
import { sanitize } from "isomorphic-dompurify";
import { marked } from "marked";
import { filterDefined, isDefined, isDefinedAndNotEmpty } from "./asserts";
import { prettyMarkdown } from "helpers/formatters";
import { filterDefined, isDefined, isDefinedAndNotEmpty } from "helpers/asserts";
export const getDescription = (
description: string | null | undefined,
@ -28,43 +26,6 @@ export const getDescription = (
return result;
};
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: {},
};
const prettyMarkdown = (markdown: string): string =>
convert(sanitize(marked(markdown, { renderer: TxtRenderer }))).trim();
const prettyChip = (items: (string | undefined)[]): string =>
items
.filter((item) => isDefined(item))

View File

@ -1,3 +1,6 @@
import { convert } from "html-to-text";
import { sanitize } from "isomorphic-dompurify";
import { marked } from "marked";
import { convertPrice } from "./numbers";
import { isDefinedAndNotEmpty, isUndefined } from "./asserts";
import { datePickerToDate } from "./date";
@ -261,3 +264,41 @@ 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(sanitize(marked(markdown, { renderer: TxtRenderer }))).trim();
};

View File

@ -79,8 +79,8 @@ const News = ({ ...otherProps }: Props): JSX.Element => {
hitsPerPage: 25,
page,
attributesToRetrieve: ["translations", "thumbnail", "slug", "date", "categories"],
attributesToHighlight: ["translations.title", "translations.excerpt", "translations.body"],
attributesToCrop: ["translations.body"],
attributesToHighlight: ["translations.title", "translations.displayable_description"],
attributesToCrop: ["translations.displayable_description"],
sort: ["sortable_date:desc"],
filter: ["hidden = false"],
});
@ -168,12 +168,10 @@ const News = ({ ...otherProps }: Props): JSX.Element => {
href={`/news/${item.slug}`}
translations={filterHasAttributes(item._formatted.translations, [
"language.data.attributes.code",
]).map(({ excerpt, body, language, ...otherAttributes }) => ({
]).map(({ excerpt, displayable_description, language, ...otherAttributes }) => ({
...otherAttributes,
description: containsHighlight(excerpt)
? excerpt
: containsHighlight(body)
? body
description: containsHighlight(displayable_description)
? displayable_description
: excerpt,
language: language.data.attributes.code,
}))}

View File

@ -18,6 +18,8 @@ export interface MeiliLibraryItem extends LibraryItemAttributesFragment {
sortable_name: string;
sortable_price: number | undefined;
sortable_date: number | undefined;
groupable_year: number | undefined;
filterable_categories: string[];
untangible_group_item: boolean;
}
@ -39,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"> {