No sources added yet.
+ diff --git a/frontend/src/types/segment.ts b/frontend/src/types/segment.ts index 8edc4f0..ac2b132 100644 --- a/frontend/src/types/segment.ts +++ b/frontend/src/types/segment.ts @@ -1,4 +1,13 @@ -export type SegmentType = "markdown" | "pdf" | "video" | "audio" | "iframe" | "gallery" | "team"; +export type SegmentType = "markdown" | "pdf" | "video" | "audio" | "iframe" | "gallery" | "team" | "sources"; + +export interface Source { + id: string; + authors: string; + year: string; + title: string; + source: string; + url: string; +} export interface Segment { id: string; diff --git a/frontend/src/utils/formatApa.ts b/frontend/src/utils/formatApa.ts new file mode 100644 index 0000000..0ca9f54 --- /dev/null +++ b/frontend/src/utils/formatApa.ts @@ -0,0 +1,37 @@ +import type { Source } from "@/types/segment"; + +function escapeHtml(text: string): string { + return text.replace(/&/g, "&").replace(//g, ">"); +} + +function formatUrl(url: string): string { + const escaped = escapeHtml(url); + return `${escaped}`; +} + +export function formatApaCitation(source: Source): string { + const parts: string[] = []; + + if (source.authors) { + const authors = escapeHtml(source.authors); + parts.push(authors.endsWith(".") ? authors : `${authors}.`); + } + + parts.push(`(${escapeHtml(source.year || "n.d.")}).`); + + if (source.title) { + const title = escapeHtml(source.title); + parts.push(title.endsWith(".") ? `${title}` : `${title}.`); + } + + if (source.source) { + const src = escapeHtml(source.source); + parts.push(src.endsWith(".") ? `${src}` : `${src}.`); + } + + if (source.url) { + parts.push(formatUrl(source.url)); + } + + return parts.join(" ").replace(/\.\./g, "."); +}