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

167 lines
3.9 KiB
JavaScript

export function base64ArrayBuffer(arrayBuffer) {
const encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
const bytes = new Uint8Array(arrayBuffer)
const byteLength = bytes.byteLength
const byteRemainder = byteLength % 3
const mainLength = byteLength - byteRemainder
let base64 = ''
let a, b, c, d
let chunk
for (let i = 0; i < mainLength; i = i + 3) {
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2]
a = (chunk & 16515072) >> 18
b = (chunk & 258048) >> 12
c = (chunk & 4032) >> 6
d = chunk & 63
base64 += encodings[a] + encodings[b] + encodings[c] + encodings[d]
}
if (byteRemainder === 1) {
chunk = bytes[mainLength]
a = (chunk & 252) >> 2
b = (chunk & 3) << 4
base64 += encodings[a] + encodings[b] + '=='
}
else if (byteRemainder === 2) {
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1]
a = (chunk & 64512) >> 10
b = (chunk & 1008) >> 4
c = (chunk & 15) << 2
base64 += encodings[a] + encodings[b] + encodings[c] + '='
}
return base64
}
export function extractFileExtension(filename) {
return filename.substr((~-filename.lastIndexOf('.') >>> 0) + 2)
}
export function eachElement(node, func) {
if (!node) return node
let result = ''
if (node.constructor === Array) {
for (let i = 0; i < node.length; i++) {
result += func(node[i], i)
}
}
else result += func(node, 0)
return result
}
export function getTextByPathList(node, path) {
if (!node) return node
for (const key of path) {
node = node[key]
if (!node) return node
}
return node
}
export function angleToDegrees(angle) {
if (!angle) return 0
return Math.round(angle / 60000)
}
export function escapeHtml(text) {
const map = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;',
}
return text.replace(/[&<>"']/g, m => map[m])
}
export function getMimeType(imgFileExt) {
let mimeType = ''
switch (imgFileExt.toLowerCase()) {
case 'jpg':
case 'jpeg':
mimeType = 'image/jpeg'
break
case 'png':
mimeType = 'image/png'
break
case 'gif':
mimeType = 'image/gif'
break
case 'emf':
mimeType = 'image/x-emf'
break
case 'wmf':
mimeType = 'image/x-wmf'
break
case 'svg':
mimeType = 'image/svg+xml'
break
case 'mp4':
mimeType = 'video/mp4'
break
case 'webm':
mimeType = 'video/webm'
break
case 'ogg':
mimeType = 'video/ogg'
break
case 'avi':
mimeType = 'video/avi'
break
case 'mpg':
mimeType = 'video/mpg'
break
case 'wmv':
mimeType = 'video/wmv'
break
case 'mp3':
mimeType = 'audio/mpeg'
break
case 'wav':
mimeType = 'audio/wav'
break
case 'tif':
mimeType = 'image/tiff'
break
case 'tiff':
mimeType = 'image/tiff'
break
default:
}
return mimeType
}
export function isVideoLink(vdoFile) {
const urlRegex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
return urlRegex.test(vdoFile)
}
export function toHex(n) {
let hex = n.toString(16)
while (hex.length < 2) {
hex = '0' + hex
}
return hex
}
export function hasValidText(htmlString) {
if (typeof DOMParser === 'undefined') {
const text = htmlString.replace(/<[^>]+>/g, '').replace(/\s+/g, ' ')
return text.trim() !== ''
}
const parser = new DOMParser()
const doc = parser.parseFromString(htmlString, 'text/html')
const text = doc.body.textContent || doc.body.innerText
return text.trim() !== ''
}
export function numberToFixed(num, fractionDigits = 4) {
return parseFloat(num.toFixed(fractionDigits))
}