Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

standardize compilers #437

Merged
merged 6 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions src/compiler/ExcalidrawCompiler.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,67 @@
import { TFile, Vault } from "obsidian";
import { Vault } from "obsidian";
import { excaliDrawBundle, excalidraw } from "../ui/suggest/constants";
import LZString from "lz-string";
import { TCompilerStep } from "./GardenPageCompiler";
import { PublishFile } from "../publisher/PublishFile";

interface IExcalidrawCompilerProps {
/**Includes excalidraw script bundle in compilation result */
includeExcaliDrawJs: boolean;
/** Is appended to the drawing id */
idAppendage?: string;
/** Includes frontmatter in compilation result */
includeFrontMatter?: boolean;
}

export class ExcalidrawCompiler {
private readonly vault: Vault;

constructor(vault: Vault) {
this.vault = vault;
}
async compileMarkdown(
{
file,
processedFrontmatter,
fileText,
}: {
file: TFile;
processedFrontmatter: string;
fileText: string;
},
includeExcaliDrawJs: boolean,
idAppendage = "",
includeFrontMatter = true,
): Promise<string> {
if (!file.name.endsWith(".excalidraw.md")) return "";

const isCompressed = fileText.includes("```compressed-json");

const start =
fileText.indexOf(isCompressed ? "```compressed-json" : "```json") +
(isCompressed ? "```compressed-json" : "```json").length;
const end = fileText.lastIndexOf("```");

const excaliDrawJson = JSON.parse(
isCompressed
? LZString.decompressFromBase64(
fileText.slice(start, end).replace(/[\n\r]/g, ""),
)
: fileText.slice(start, end),
);

const drawingId =
file.name.split(" ").join("_").replace(".", "") + idAppendage;
let excaliDrawCode = "";

if (includeExcaliDrawJs) {
excaliDrawCode += excaliDrawBundle;
}

excaliDrawCode += excalidraw(JSON.stringify(excaliDrawJson), drawingId);

return `${
includeFrontMatter ? processedFrontmatter : ""
}${excaliDrawCode}`;
}
compileMarkdown =
({
includeExcaliDrawJs,
idAppendage = "",
includeFrontMatter = true,
}: IExcalidrawCompilerProps): TCompilerStep =>
(file: PublishFile) =>
(fileText: string) => {
if (!file.file.name.endsWith(".excalidraw.md")) {
throw new Error("File is not an excalidraw file");
}

const isCompressed = fileText.includes("```compressed-json");
const startString = isCompressed ? "```compressed-json" : "```json";

const start = fileText.indexOf(startString) + startString.length;
const end = fileText.lastIndexOf("```");

const excaliDrawJson = JSON.parse(
isCompressed
? LZString.decompressFromBase64(
fileText.slice(start, end).replace(/[\n\r]/g, ""),
)
: fileText.slice(start, end),
);

const drawingId =
file.file.name.split(" ").join("_").replace(".", "") +
idAppendage;

let excaliDrawCode = "";

if (includeExcaliDrawJs) {
excaliDrawCode += excaliDrawBundle;
}

excaliDrawCode += excalidraw(
JSON.stringify(excaliDrawJson),
drawingId,
);

return `${
includeFrontMatter ? file.getCompiledFrontmatter() : ""
}${excaliDrawCode}`;
};
}
7 changes: 2 additions & 5 deletions src/compiler/FrontmatterCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@ export class FrontmatterCompiler {
const frontmatter =
metadataCache.getCache(file.path)?.frontmatter ?? {};

return this.getProcessedFrontMatter(file, frontmatter);
return this.compile(file, frontmatter);
}

getProcessedFrontMatter(
file: TFile,
frontmatter: FrontMatterCache,
): string {
compile(file: TFile, frontmatter: FrontMatterCache): string {
const fileFrontMatter = { ...frontmatter };
delete fileFrontMatter["position"];

Expand Down
Loading