-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Custom preamble #79
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
Open
Splines
wants to merge
4
commits into
main
Choose a base branch
from
preamble
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,25 +1,90 @@ | ||
| import { decodeBase64, encodeBase64 } from "./utils/base64"; | ||
| import { decodeBase64 } from "./utils/base64"; | ||
| import { SHAPE_CONFIG } from "./constants.js"; | ||
|
|
||
| const TYPST_PREFIX = "TYPST:"; | ||
| const LEGACY_TYPST_PREFIX = "TYPST:"; | ||
|
|
||
| export type TypstSource = { | ||
| preamble: string; | ||
| body: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a Typst payload from source code. | ||
| * Checks if an alt text description contains a legacy Typst payload. | ||
| */ | ||
| export function createTypstPayload(code: string): string { | ||
| return `${TYPST_PREFIX}${encodeBase64(code)}`; | ||
| export function isLegacyTypstPayload(altTextDescription: string | undefined): boolean { | ||
| return !!(altTextDescription && altTextDescription.startsWith(LEGACY_TYPST_PREFIX)); | ||
| } | ||
|
|
||
| /** | ||
| * Checks if an alt text description is a Typst payload. | ||
| * Extracts Typst code from a legacy payload. | ||
| */ | ||
| export function isTypstPayload(altTextDescription: string | undefined): boolean { | ||
| return !!(altTextDescription && altTextDescription.startsWith(TYPST_PREFIX)); | ||
| export function extractLegacyTypstCode(payload: string): string { | ||
| const base64Payload = payload.split(LEGACY_TYPST_PREFIX)[1]; | ||
| return decodeBase64(base64Payload); | ||
| } | ||
|
|
||
| /** | ||
| * Extracts Typst code from a payload. | ||
| * Converts legacy payload data to the current Typst source structure. | ||
| */ | ||
| export function extractTypstCode(payload: string): string { | ||
| const base64Payload = payload.split(TYPST_PREFIX)[1]; | ||
| return decodeBase64(base64Payload); | ||
| export function getLegacyTypstSource(payload: string): TypstSource { | ||
| return { | ||
| preamble: "", | ||
| body: extractLegacyTypstCode(payload), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Serializes Typst source for storage in a shape-scoped custom XML part. | ||
| */ | ||
| export function serializeTypstSource(source: TypstSource): string { | ||
| const documentNode = document.implementation.createDocument( | ||
| SHAPE_CONFIG.CUSTOM_XML.NAMESPACE, | ||
| SHAPE_CONFIG.CUSTOM_XML.ROOT, | ||
| null, | ||
| ); | ||
| const root = documentNode.documentElement; | ||
|
|
||
| const preambleNode = documentNode.createElementNS( | ||
| SHAPE_CONFIG.CUSTOM_XML.NAMESPACE, | ||
| SHAPE_CONFIG.CUSTOM_XML.PREAMBLE_NODE, | ||
| ); | ||
| preambleNode.textContent = source.preamble; | ||
|
|
||
| const bodyNode = documentNode.createElementNS( | ||
| SHAPE_CONFIG.CUSTOM_XML.NAMESPACE, | ||
| SHAPE_CONFIG.CUSTOM_XML.BODY_NODE, | ||
| ); | ||
| bodyNode.textContent = source.body; | ||
|
|
||
| root.appendChild(preambleNode); | ||
| root.appendChild(bodyNode); | ||
|
|
||
| return new XMLSerializer().serializeToString(documentNode); | ||
| } | ||
|
|
||
| /** | ||
| * Parses Typst source from a shape-scoped custom XML part. | ||
| */ | ||
| export function parseTypstSource(xml: string): TypstSource | null { | ||
| const documentNode = new DOMParser().parseFromString(xml, "application/xml"); | ||
| if (documentNode.querySelector("parsererror")) { | ||
| return null; | ||
| } | ||
|
|
||
| const preambleNode = documentNode | ||
| .getElementsByTagNameNS(SHAPE_CONFIG.CUSTOM_XML.NAMESPACE, "preamble") | ||
| .item(0); | ||
|
|
||
| const bodyNode = documentNode | ||
| .getElementsByTagNameNS(SHAPE_CONFIG.CUSTOM_XML.NAMESPACE, "body") | ||
| .item(0); | ||
|
|
||
| if (preambleNode === null || bodyNode === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| preamble: preambleNode.textContent, | ||
| body: bodyNode.textContent, | ||
| }; | ||
|
Splines marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.