Files
simstudioai--sim/apps/sim/lib/pptx-renderer/parser/rel-parser.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

82 lines
2.1 KiB
TypeScript

/**
* Parser for .rels (Relationship) XML files in OOXML packages.
* These files map relationship IDs (rId1, rId2, ...) to targets.
*/
import { parseXml } from './xml-parser'
export interface RelEntry {
type: string
target: string
targetMode?: string
}
/**
* Parse a .rels XML string into a Map of relationship ID -> RelEntry.
*
* Example input:
* ```xml
* <Relationships xmlns="...">
* <Relationship Id="rId1" Type="http://...slide" Target="slides/slide1.xml"/>
* </Relationships>
* ```
*/
export function parseRels(xmlString: string): Map<string, RelEntry> {
const result = new Map<string, RelEntry>()
if (!xmlString) return result
const root = parseXml(xmlString)
if (!root.exists()) return result
const relationships = root.children('Relationship')
for (const rel of relationships) {
const id = rel.attr('Id')
const type = rel.attr('Type')
const target = rel.attr('Target')
const targetMode = rel.attr('TargetMode')
if (id && type !== undefined && target !== undefined) {
result.set(id, { type, target, targetMode })
}
}
return result
}
/**
* Resolve a relative target path against a base path.
*
* Examples:
* resolveRelTarget('ppt/slides', '../slideLayouts/slideLayout1.xml')
* → 'ppt/slideLayouts/slideLayout1.xml'
*
* resolveRelTarget('ppt/slides', 'media/image1.png')
* → 'ppt/slides/media/image1.png'
*
* resolveRelTarget('ppt', 'slides/slide1.xml')
* → 'ppt/slides/slide1.xml'
*/
export function resolveRelTarget(basePath: string, target: string): string {
// Absolute targets (start with /) are returned as-is (strip leading /)
if (target.startsWith('/')) {
return target.slice(1)
}
// Split the base path into segments
const baseParts = basePath.replace(/\\/g, '/').split('/').filter(Boolean)
const targetParts = target.replace(/\\/g, '/').split('/').filter(Boolean)
// Walk through target parts, resolving '..' by popping base parts
const resolved = [...baseParts]
for (const part of targetParts) {
if (part === '..') {
resolved.pop()
} else if (part !== '.') {
resolved.push(part)
}
}
return resolved.join('/')
}