-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
cae58b3
commit 2c39f03
Showing
11 changed files
with
227 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.