37 lines
946 B
JavaScript
37 lines
946 B
JavaScript
import Mustache from "mustache";
|
|
import {stripHtml} from "../../helpers";
|
|
import {getContext} from "svelte";
|
|
|
|
export function previewTitle(record) {
|
|
const channel = getContext("channel");
|
|
let schema = channel.schemas.find((aSchema) => aSchema.name === record?.schema);
|
|
|
|
if (!schema?.titleTemplate) {
|
|
return noTemplate(schema, record);
|
|
}
|
|
|
|
let template = Mustache.parse(schema.titleTemplate);
|
|
let render = Mustache.render(schema.titleTemplate, record.data);
|
|
|
|
if (!render || render === "") {
|
|
return noTemplate(schema, record);
|
|
}
|
|
|
|
return stripHtml(render.slice(0, 300));
|
|
}
|
|
|
|
function noTemplate(schema, record) {
|
|
if (schema?.type === "files") {
|
|
return record._file.path;
|
|
}
|
|
|
|
let title = stripHtml(
|
|
record?.data[schema.fields.filter((f) => f.info.name === "text")[0]?.name]
|
|
).slice(0, 300);
|
|
if (title === "") {
|
|
return "Untitled";
|
|
}
|
|
|
|
return title;
|
|
}
|