Skip to content

Commit

Permalink
standardize compilers (#437)
Browse files Browse the repository at this point in the history
* compiler: standardize compiler function

* compiler: fp fc yeah

* chore: mild refactor

* chore: change compiler type

* chore: cleanup excalidraw compiler

* chore: fix merge issues
  • Loading branch information
julesvirallinen authored Oct 1, 2023
1 parent cae58b3 commit 2c39f03
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 187 deletions.
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

0 comments on commit 2c39f03

Please sign in to comment.