artifacty 0.1.2 → 0.3.0

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.
@@ -15,7 +15,7 @@ const messages = {
15
15
  ...(globalThis.ARTIFACTY_I18N || {})
16
16
  };
17
17
 
18
- const SUPPORTED_FORMATS = ["markdown", "html", "json", "text", "code", "svg", "mermaid", "react"];
18
+ const SUPPORTED_FORMATS = ["markdown", "html", "json", "text", "code", "svg", "mermaid", "react", "sarif", "csv", "image", "video"];
19
19
 
20
20
  const editorTheme = EditorView.theme({
21
21
  "&": {
@@ -128,7 +128,7 @@ function enhanceTextarea(textarea) {
128
128
  updatePreview();
129
129
 
130
130
  function updateToolbar(format) {
131
- formatJsonButton.hidden = format !== "json";
131
+ formatJsonButton.hidden = format !== "json" && format !== "sarif";
132
132
  status.textContent = formatLabel(format);
133
133
  }
134
134
 
@@ -158,7 +158,7 @@ function enhanceTextarea(textarea) {
158
158
  return;
159
159
  }
160
160
 
161
- if (format === "json") {
161
+ if (format === "json" || format === "sarif") {
162
162
  const pre = document.createElement("pre");
163
163
  try {
164
164
  pre.textContent = JSON.stringify(JSON.parse(content), null, 2);
@@ -191,7 +191,7 @@ function languageExtension(format) {
191
191
  if (format === "html" || format === "svg") {
192
192
  return html();
193
193
  }
194
- if (format === "json") {
194
+ if (format === "json" || format === "sarif") {
195
195
  return json();
196
196
  }
197
197
  if (format === "markdown") {
@@ -212,6 +212,18 @@ function detectFormat({ explicit, fileName, content }) {
212
212
  if (lowerName.endsWith(".md") || lowerName.endsWith(".markdown")) {
213
213
  return "markdown";
214
214
  }
215
+ if (lowerName.endsWith(".sarif") || lowerName.endsWith(".sarif.json")) {
216
+ return "sarif";
217
+ }
218
+ if (lowerName.endsWith(".csv")) {
219
+ return "csv";
220
+ }
221
+ if (/\.(png|jpe?g|gif|webp)$/.test(lowerName)) {
222
+ return "image";
223
+ }
224
+ if (/\.(mp4|webm)$/.test(lowerName)) {
225
+ return "video";
226
+ }
215
227
  if (lowerName.endsWith(".json")) {
216
228
  return "json";
217
229
  }
@@ -241,6 +253,18 @@ function detectFormat({ explicit, fileName, content }) {
241
253
  if (trimmed.startsWith("<!doctype") || trimmed.startsWith("<html") || trimmed.startsWith("<")) {
242
254
  return "html";
243
255
  }
256
+ if (looksLikeSarif(trimmed)) {
257
+ return "sarif";
258
+ }
259
+ if (looksLikeCsv(trimmed)) {
260
+ return "csv";
261
+ }
262
+ if (/^data:image\/(?:png|jpeg|gif|webp);base64,/i.test(trimmed)) {
263
+ return "image";
264
+ }
265
+ if (/^data:video\/(?:mp4|webm);base64,/i.test(trimmed)) {
266
+ return "video";
267
+ }
244
268
  if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
245
269
  return "json";
246
270
  }
@@ -255,6 +279,54 @@ function formatLabel(format) {
255
279
  return messages.mode.replaceAll("{format}", label);
256
280
  }
257
281
 
282
+ function looksLikeSarif(value) {
283
+ const trimmed = String(value || "").trim();
284
+ if (!trimmed.startsWith("{")) {
285
+ return false;
286
+ }
287
+ try {
288
+ const parsed = JSON.parse(trimmed);
289
+ return parsed &&
290
+ typeof parsed === "object" &&
291
+ Array.isArray(parsed.runs) &&
292
+ (typeof parsed.version === "string" || String(parsed.$schema || "").toLowerCase().includes("sarif"));
293
+ } catch {
294
+ return false;
295
+ }
296
+ }
297
+
298
+ function looksLikeCsv(value) {
299
+ const lines = String(value || "")
300
+ .trim()
301
+ .split(/\r?\n/)
302
+ .map((line) => line.trimEnd())
303
+ .filter(Boolean)
304
+ .slice(0, 5);
305
+ if (lines.length < 2 || lines[0].trimStart().startsWith("|")) {
306
+ return false;
307
+ }
308
+ const counts = lines.map(csvFieldCount);
309
+ return counts[0] > 1 && counts.every((count) => count === counts[0]);
310
+ }
311
+
312
+ function csvFieldCount(line) {
313
+ let count = 1;
314
+ let inQuotes = false;
315
+ for (let index = 0; index < line.length; index += 1) {
316
+ const char = line[index];
317
+ if (char === "\"") {
318
+ if (inQuotes && line[index + 1] === "\"") {
319
+ index += 1;
320
+ } else {
321
+ inQuotes = !inQuotes;
322
+ }
323
+ } else if (char === "," && !inQuotes) {
324
+ count += 1;
325
+ }
326
+ }
327
+ return count;
328
+ }
329
+
258
330
  function invalidJsonMessage(error) {
259
331
  return messages.invalidJson.replaceAll("{message}", error.message);
260
332
  }