Files
outline/server/converters/BaseConverter.ts
T
Tom MoorandClaude Fable 5 022f442162 feat: Add Open Knowledge Format (OKF) import support (#13580)
* feat: Add Open Knowledge Format (OKF) import support

Maps the OKF service onto the existing Markdown zip import pipeline.
Document titles are now read from YAML frontmatter when present, for
all Markdown-based imports.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* Switch to Globe icon

* Strip YAML frontmatter on import, derive import service labels from plugins

- Remove frontmatter from the document body instead of converting it to a YAML codeblock
- Drop a leading H1 that repeats the frontmatter title
- Read plugin importer labels from their Hook.Imports registration

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-10-10 14:25:02 +00:00

69 lines
1.9 KiB
TypeScript

import yaml from "js-yaml";
/**
* Base class for the individual file format converters, holding the handling
* that is common to reading any incoming file.
*/
export abstract class BaseConverter {
/**
* Convert a Buffer to a string.
*
* @param content The content as a Buffer or string.
* @returns The content as a string.
*/
protected static bufferToString(content: Buffer | string): string {
return typeof content === "string" ? content : content.toString("utf8");
}
/**
* Parse YAML frontmatter at the start of the content, if present.
*
* @param content The markdown content that may contain frontmatter.
* @returns The parsed frontmatter as an object, or undefined when no valid
* frontmatter is present.
*/
protected static parseFrontmatter(
content: string
): Record<string, unknown> | undefined {
const match = content.match(this.frontmatterRegex);
if (!match) {
return undefined;
}
try {
const data: unknown = yaml.load(match[1]);
if (data && typeof data === "object" && !Array.isArray(data)) {
return data as Record<string, unknown>;
}
return undefined;
} catch {
return undefined;
}
}
/**
* Remove YAML frontmatter from the start of the content, if present.
* Content that is not valid YAML is not frontmatter and is left in place.
*
* @param content The markdown content that may contain frontmatter.
* @returns The markdown content without frontmatter.
*/
protected static stripFrontmatter(content: string): string {
const match = content.match(this.frontmatterRegex);
if (!match) {
return content;
}
try {
yaml.load(match[1]);
} catch {
return content;
}
return content.slice(match[0].length);
}
// Frontmatter must start at the beginning of the document
private static readonly frontmatterRegex = /^---\n([\s\S]*?)\n---(?:\n|$)/;
}