Public
Edited
Sep 3, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
parsed = {
try {
return JSON.parse(input);
} catch {
return {};
}
}
Insert cell
markdown = {
if (!parsed.chat_messages) {
return "";
}
const bits = [];
bits.push(`# ${parsed.name}`);
parsed.chat_messages.forEach((message) => {
bits.push(
`<mark>**${message.sender}**</mark> (${new Date(message.created_at).toLocaleString(
"en-US",
{
month: "short",
day: "numeric",
year: "numeric",
hour: "2-digit",
minute: "2-digit"
}
)})`
);
bits.push(
replaceArtifactTags(message.text.replace(/<\/antArtifact>/g, "\n```"))
);
});
return bits.join("\n\n");
}
Insert cell
function escapeHtml(str) {
return str.replace(/[&<>"']/g, function (match) {
const entityMap = {
"&": "&amp;",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#39;"
};
return entityMap[match];
});
}
Insert cell
function replaceArtifactTags(input) {
// Regular expression to match <antArtifact> tags
const regex = /<antArtifact[^>]*>/g;

// Function to extract attributes from a tag string
function extractAttributes(tag) {
const attributes = {};
const attrRegex = /(\w+)=("([^"]*)"|'([^']*)')/g;
let match;
while ((match = attrRegex.exec(tag)) !== null) {
const key = match[1];
const value = match[3] || match[4]; // Use either double or single quotes
attributes[key] = value;
}
return attributes;
}

return input.replace(regex, (match) => {
const attributes = extractAttributes(match);
const typeLookup = {
"application/vnd.ant.react": "jsx",
"text/html": "html"
};
// Determine language based on 'language' attribute, otherwise fallback logic
const lang = attributes.language || typeLookup[attributes.type] || "";

// Return the Markdown formatted string
return `### ${attributes.title || "Untitled"}\n\n\`\`\`${lang}`;
});
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more