@ai-matrx/print 0.8.2 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/markdown.cjs CHANGED
@@ -38,7 +38,8 @@ __export(markdown_exports, {
38
38
  printMarkdown: () => printMarkdown,
39
39
  removeThinkingContent: () => removeThinkingContent,
40
40
  renderMarkdownDocument: () => renderMarkdownDocument,
41
- splitAtPageBreaks: () => splitAtPageBreaks
41
+ splitAtPageBreaks: () => splitAtPageBreaks,
42
+ unwrapArtifactsToMarkdown: () => unwrapArtifactsToMarkdown
42
43
  });
43
44
  module.exports = __toCommonJS(markdown_exports);
44
45
 
@@ -1206,6 +1207,44 @@ function artifactLabel(type) {
1206
1207
  if (!type) return "Artifact";
1207
1208
  return ARTIFACT_TYPE_LABEL[type] ?? type.replace(/[-_]+/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
1208
1209
  }
1210
+ function unwrapArtifactsToMarkdown(markdown) {
1211
+ if (!/<artifact\b/i.test(markdown)) return markdown;
1212
+ return markdown.split(/(^[ \t]*(?:`{3,}|~{3,})[\s\S]*?^[ \t]*(?:`{3,}|~{3,})[ \t]*$)/m).map(
1213
+ (part, i) => i % 2 === 1 ? part : part.replace(/<artifact\b((?:[^>"']|"[^"]*"|'[^']*')*?)>([\s\S]*?)<\/artifact>/gi, (_m, tag, body) => {
1214
+ const attrs = parseTagAttributes(tag);
1215
+ const type = (attrs.type ?? "").toLowerCase();
1216
+ const trimmed = body.replace(/^\n+|\n+$/g, "");
1217
+ if (!trimmed.trim()) return "";
1218
+ if (/^\s*(`{3,}|~{3,})/.test(trimmed)) return `
1219
+
1220
+ ${trimmed}
1221
+
1222
+ `;
1223
+ if (type in CODE_ARTIFACT_LANGUAGE) {
1224
+ const lang = CODE_ARTIFACT_LANGUAGE[type] || attrs.language || attrs.lang || "";
1225
+ return `
1226
+
1227
+ \`\`\`\`${lang}
1228
+ ${trimmed}
1229
+ \`\`\`\`
1230
+
1231
+ `;
1232
+ }
1233
+ if (looksLikeJson(trimmed)) return `
1234
+
1235
+ \`\`\`\`json
1236
+ ${JSON.stringify(JSON.parse(trimmed), null, 2)}
1237
+ \`\`\`\`
1238
+
1239
+ `;
1240
+ return `
1241
+
1242
+ ${trimmed}
1243
+
1244
+ `;
1245
+ })
1246
+ ).join("");
1247
+ }
1209
1248
  function looksLikeJson(body) {
1210
1249
  const t = body.trim();
1211
1250
  if (!/^[[{]/.test(t)) return false;