send-format.mjs 1.0 KB

123456789101112131415161718192021222324252627
  1. // Outbound /send builder selection for the Photon sidecar.
  2. //
  3. // spectrumMarkdown() enables data detection (enableDataDetection) in the
  4. // underlying iMessage API, which can 500 on messages containing raw URLs.
  5. // Plain-text URLs are auto-linked by iMessage anyway, so markdown messages
  6. // that contain a URL are routed through the text builder, while URL-free
  7. // markdown keeps native markdown rendering.
  8. //
  9. // This lives in its own module (rather than inline in index.mjs) so tests can
  10. // execute the real decision logic under node instead of grepping source —
  11. // see tests/plugins/platforms/photon/test_url_send_path.py.
  12. const URL_RE = /https?:\/\/[^\s)'"<>]+/i;
  13. /**
  14. * Decide which spectrum-ts builder the /send handler should use.
  15. *
  16. * @param {string} format "markdown" | "text" (already validated by /send)
  17. * @param {string} text the outbound message body
  18. * @returns {"markdown"|"text"}
  19. */
  20. export function chooseSendFormat(format, text) {
  21. if (format === "markdown" && !URL_RE.test(String(text))) {
  22. return "markdown";
  23. }
  24. return "text";
  25. }