wyuc 7cb129183d
chore(packages): publish the @openmaic/* SDK family to npm (#778) (#780)
* chore(packages): publish the @openmaic/* SDK family to npm (#778)

Prepares the @openmaic/{dsl,renderer,importer} family for its first npm
publish, and moves the SDK packages onto the @openmaic scope.

Why the scope move: the @maic org name is unavailable on npm (an unscoped
`maic` package already holds the name), so @maic/* is not claimable. @openmaic
matches the project name, the scope is free, and the repo already ships an
@openmaic/docs package — so the SDK family now lines up with that convention.

- rename @maic/{dsl,renderer,importer} -> @openmaic/* across packages, the
  workspace glob, the package dir, and all import sites; lockfile regenerated
- renderer: add publishConfig (public, registry.npmjs.org) — was missing, so
  a scoped publish would default to the wrong registry / restricted access
- importer: add a files allowlist (dist, README, LICENSE) and drop the
  fragile .npmignore blacklist that shipped src; add an exports map so ESM
  consumers resolve dist/index.js instead of falling back to the .cjs main
- all three: add a prepublishOnly build (+ test/typecheck) guard so a publish
  can never ship a stale or empty dist
- add a tag-triggered publish workflow with npm provenance, pinned by name to
  the three @openmaic packages so the vendored forks (mathml2omml, pptxgenjs)
  are never published

Refs #778, #720 (Phase 1).

* fix(packages): address cross-review on the @openmaic publish prep

Cross-review (Claude /code-review + codex) on this PR surfaced:

- renderer's advertised CJS entry was broken: it keeps @openmaic/dsl external
  and imports a runtime enum from it, but dsl is ESM-only (no `require`
  condition), so `require('@openmaic/renderer')` would throw
  ERR_PACKAGE_PATH_NOT_EXPORTED. Make renderer ESM-only: drop the `.cjs`
  rollup output, `main` now points at the ESM build, and the `require`
  conditions are removed from `exports`. (importer is unaffected — it bundles
  dsl, so its CJS build still works.)
- prepublishOnly re-ran the test suite during `pnpm -r publish`, so a flaky
  test after dsl had already published gave a non-atomic partial release.
  Reduce prepublishOnly to a build-only guard (never ship stale/empty dist)
  and move the real test/typecheck gate into the workflow, before any publish.
- document that an @openmaic/* tag publishes the whole family via `pnpm -r`
  (pnpm skips already-published versions); the tag is a release marker, not a
  per-package gate.

Verified: dsl + renderer + importer build; renderer emits ESM only (0 .cjs),
all exports entries resolve; `npm pack` ships dist + README + LICENSE with no
src leak; frozen-lockfile passes.

Refs #778.

* style: reflow @openmaic/dsl type imports past print-width after rename

The @maic -> @openmaic rename lengthened two single-line type imports past
prettier's 100-col width; prettier --check flagged them. Pure formatting.

Refs #778.

* docs(importer): mark @openmaic/importer browser-only (cr-loop accepted limitation)

codex cross-review flagged that the published @openmaic/importer throws
`XMLHttpRequest is not a constructor` when loaded in a pure Node process —
its rollup build is browser-targeted (`nodeResolve({browser:true})` + a
browser pdf.js build). The app only consumes it client-side ('use client'),
so this is by design. Document it as an accepted limitation: prominent
browser-only note in the README and a `browser` field in the manifest.

Refs #778.
2026-06-24 15:12:23 +08:00

214 lines
7.8 KiB
JavaScript

import { getTextByPathList } from './utils'
import { getShadow } from './shadow'
import { getFillType, getGradientFill, getSolidFill } from './fill'
export function getFontType(node, type, warpObj, slideLayoutSpNode, slideMasterSpNode, slideMasterTextStyles) {
const extractFont = (targetNode, isDirectRun = false) => {
if (!targetNode) return null
let rPr
if (isDirectRun) rPr = getTextByPathList(targetNode, ['a:rPr'])
else {
rPr = getTextByPathList(targetNode, ['p:txBody', 'a:lstStyle', 'a:lvl1pPr', 'a:defRPr'])
if (!rPr) rPr = getTextByPathList(targetNode, ['p:txBody', 'a:p', 'a:pPr', 'a:defRPr'])
}
if (!rPr) return null
return getTextByPathList(rPr, ['a:latin', 'attrs', 'typeface']) || getTextByPathList(rPr, ['a:ea', 'attrs', 'typeface'])
}
let typeface = extractFont(node, true)
if (!typeface) typeface = extractFont(slideLayoutSpNode)
if (!typeface) typeface = extractFont(slideMasterSpNode)
if (!typeface) {
let stylePath = []
if (type === 'title' || type === 'ctrTitle' || type === 'subTitle') {
stylePath = ['p:titleStyle', 'a:lvl1pPr', 'a:defRPr']
}
else if (type === 'body') {
stylePath = ['p:bodyStyle', 'a:lvl1pPr', 'a:defRPr']
}
else {
stylePath = ['p:otherStyle', 'a:lvl1pPr', 'a:defRPr']
}
const masterGlobalRPr = getTextByPathList(slideMasterTextStyles, stylePath)
if (masterGlobalRPr) {
typeface = getTextByPathList(masterGlobalRPr, ['a:latin', 'attrs', 'typeface']) || getTextByPathList(masterGlobalRPr, ['a:ea', 'attrs', 'typeface'])
}
}
if (!typeface || typeface.startsWith('+')) {
const fontSchemeNode = getTextByPathList(warpObj['themeContent'], ['a:theme', 'a:themeElements', 'a:fontScheme'])
if (fontSchemeNode) {
if (typeface && typeface.startsWith('+')) {
switch (typeface) {
case '+mj-lt':
return getTextByPathList(fontSchemeNode, ['a:majorFont', 'a:latin', 'attrs', 'typeface'])
case '+mn-lt':
return getTextByPathList(fontSchemeNode, ['a:minorFont', 'a:latin', 'attrs', 'typeface'])
case '+mj-ea':
return getTextByPathList(fontSchemeNode, ['a:majorFont', 'a:ea', 'attrs', 'typeface'])
case '+mn-ea':
return getTextByPathList(fontSchemeNode, ['a:minorFont', 'a:ea', 'attrs', 'typeface'])
default:
return typeface.replace(/^\+/, '')
}
}
}
if (type === 'title' || type === 'subTitle' || type === 'ctrTitle') {
typeface = getTextByPathList(fontSchemeNode, ['a:majorFont', 'a:latin', 'attrs', 'typeface']) || getTextByPathList(fontSchemeNode, ['a:majorFont', 'a:ea', 'attrs', 'typeface'])
}
else {
typeface = getTextByPathList(fontSchemeNode, ['a:minorFont', 'a:latin', 'attrs', 'typeface'])
}
}
return typeface || ''
}
export function getFontColor(node, pNode, lstStyle, pFontStyle, lvl, warpObj) {
const rPrNode = getTextByPathList(node, ['a:rPr'])
let filTyp, color
if (rPrNode) {
filTyp = getFillType(rPrNode)
if (filTyp === 'SOLID_FILL') {
const solidFillNode = rPrNode['a:solidFill']
color = getSolidFill(solidFillNode, undefined, undefined, warpObj)
}
if (filTyp === 'GRADIENT_FILL') {
const gradientFillNode = rPrNode['a:gradFill']
const gradient = getGradientFill(gradientFillNode, warpObj)
return gradient
}
}
if (!color && getTextByPathList(lstStyle, ['a:lvl' + lvl + 'pPr', 'a:defRPr'])) {
const lstStyledefRPr = getTextByPathList(lstStyle, ['a:lvl' + lvl + 'pPr', 'a:defRPr'])
filTyp = getFillType(lstStyledefRPr)
if (filTyp === 'SOLID_FILL') {
const solidFillNode = lstStyledefRPr['a:solidFill']
color = getSolidFill(solidFillNode, undefined, undefined, warpObj)
}
}
if (!color) {
const sPstyle = getTextByPathList(pNode, ['p:style', 'a:fontRef'])
if (sPstyle) color = getSolidFill(sPstyle, undefined, undefined, warpObj)
if (!color && pFontStyle) color = getSolidFill(pFontStyle, undefined, undefined, warpObj)
}
return color || ''
}
export function getFontSize(node, slideLayoutSpNode, type, slideMasterTextStyles, textBodyNode, pNode) {
let fontSize
if (getTextByPathList(node, ['a:rPr', 'attrs', 'sz'])) fontSize = getTextByPathList(node, ['a:rPr', 'attrs', 'sz']) / 100
if ((isNaN(fontSize) || !fontSize) && pNode) {
if (getTextByPathList(pNode, ['a:endParaRPr', 'attrs', 'sz'])) {
fontSize = getTextByPathList(pNode, ['a:endParaRPr', 'attrs', 'sz']) / 100
}
}
if ((isNaN(fontSize) || !fontSize) && textBodyNode) {
const lstStyle = getTextByPathList(textBodyNode, ['a:lstStyle'])
if (lstStyle) {
let lvl = 1
if (pNode) {
const lvlNode = getTextByPathList(pNode, ['a:pPr', 'attrs', 'lvl'])
if (lvlNode !== undefined) lvl = parseInt(lvlNode) + 1
}
const sz = getTextByPathList(lstStyle, [`a:lvl${lvl}pPr`, 'a:defRPr', 'attrs', 'sz'])
if (sz) fontSize = parseInt(sz) / 100
}
}
if ((isNaN(fontSize) || !fontSize)) {
const sz = getTextByPathList(slideLayoutSpNode, ['p:txBody', 'a:lstStyle', 'a:lvl1pPr', 'a:defRPr', 'attrs', 'sz'])
if (sz) fontSize = parseInt(sz) / 100
}
if ((isNaN(fontSize) || !fontSize) && slideLayoutSpNode) {
let lvl = 1
if (pNode) {
const lvlNode = getTextByPathList(pNode, ['a:pPr', 'attrs', 'lvl'])
if (lvlNode !== undefined) lvl = parseInt(lvlNode) + 1
}
const layoutSz = getTextByPathList(slideLayoutSpNode, ['p:txBody', 'a:lstStyle', `a:lvl${lvl}pPr`, 'a:defRPr', 'attrs', 'sz'])
if (layoutSz) fontSize = parseInt(layoutSz) / 100
}
if ((isNaN(fontSize) || !fontSize) && pNode) {
const paraSz = getTextByPathList(pNode, ['a:pPr', 'a:defRPr', 'attrs', 'sz'])
if (paraSz) fontSize = parseInt(paraSz) / 100
}
if (isNaN(fontSize) || !fontSize) {
let sz
if (type === 'title' || type === 'subTitle' || type === 'ctrTitle') {
sz = getTextByPathList(slideMasterTextStyles, ['p:titleStyle', 'a:lvl1pPr', 'a:defRPr', 'attrs', 'sz'])
}
else if (type === 'body') {
sz = getTextByPathList(slideMasterTextStyles, ['p:bodyStyle', 'a:lvl1pPr', 'a:defRPr', 'attrs', 'sz'])
}
else if (type === 'dt' || type === 'sldNum') {
sz = '1200'
}
else if (!type) {
sz = getTextByPathList(slideMasterTextStyles, ['p:otherStyle', 'a:lvl1pPr', 'a:defRPr', 'attrs', 'sz'])
}
if (sz) fontSize = parseInt(sz) / 100
}
const baseline = getTextByPathList(node, ['a:rPr', 'attrs', 'baseline'])
if (baseline && !isNaN(fontSize)) fontSize -= 10
fontSize = (isNaN(fontSize) || !fontSize) ? 18 : fontSize
return fontSize + 'pt'
}
export function getFontBold(node) {
return getTextByPathList(node, ['a:rPr', 'attrs', 'b']) === '1' ? 'bold' : ''
}
export function getFontItalic(node) {
return getTextByPathList(node, ['a:rPr', 'attrs', 'i']) === '1' ? 'italic' : ''
}
export function getFontDecoration(node) {
return getTextByPathList(node, ['a:rPr', 'attrs', 'u']) === 'sng' ? 'underline' : ''
}
export function getFontDecorationLine(node) {
return getTextByPathList(node, ['a:rPr', 'attrs', 'strike']) === 'sngStrike' ? 'line-through' : ''
}
export function getFontSpace(node) {
const spc = getTextByPathList(node, ['a:rPr', 'attrs', 'spc'])
return spc ? (parseInt(spc) / 100 + 'pt') : ''
}
export function getFontSubscript(node) {
const baseline = getTextByPathList(node, ['a:rPr', 'attrs', 'baseline'])
if (!baseline) return ''
return parseInt(baseline) > 0 ? 'super' : 'sub'
}
export function getFontShadow(node, warpObj) {
const txtShadow = getTextByPathList(node, ['a:rPr', 'a:effectLst', 'a:outerShdw'])
if (txtShadow) {
const shadow = getShadow(txtShadow, warpObj)
if (shadow) {
const { h, v, blur, color } = shadow
if (!isNaN(v) && !isNaN(h)) {
return h + 'pt ' + v + 'pt ' + (blur ? blur + 'pt' : '') + ' ' + color
}
}
}
return ''
}