Skip to content

Commit

Permalink
Refactor getFicheById API endpoint to include a truncated description
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Mar 5, 2024
1 parent 6841b4d commit 45e8526
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/pages/api/[lang]/getFicheById/[id].ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
import { getCollection, type CollectionEntry } from "astro:content";
import type { EntryType } from "perf_hooks";
import { marked } from "marked";
import { getRefConfig } from "referentiel-config";

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 = {
// 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: {},
};

export async function GET({ params, request }) {
const id = params.id;
const lang = params.lang;
Expand All @@ -15,6 +49,9 @@ export async function GET({ params, request }) {
}
const entry = filteredEntries[0];
const [_lang, ...slug] = entry.slug.split("/");
const flatBody =
(await marked(entry.body, { renderer: TxtRenderer })).slice(0, 500) + "...";

let o = {
id: entry.data.refID,
lang: lang,
Expand All @@ -25,6 +62,7 @@ export async function GET({ params, request }) {
if (getRefConfig().featuresEnabled.scope) {
o["scope"] = entry.data.scope;
}
o["description"] = flatBody;
return new Response(JSON.stringify(o), {
status: 200,
headers: {
Expand Down

0 comments on commit 45e8526

Please sign in to comment.