add missing lib files

This commit is contained in:
Matthias Nannt
2023-08-22 14:45:11 +02:00
parent 8c229e4685
commit b0e42e00e9
4 changed files with 178 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
import glob from "fast-glob";
import * as path from "path";
async function importArticle(articleFilename: string) {
let { meta, default: component } = await import(`../pages/blog/${articleFilename}`);
return {
slug: articleFilename.replace(/(\/index)?\.mdx$/, ""),
...meta,
component,
};
}
export async function getAllArticles() {
let articleFilenames = await glob(["*.mdx", "*/index.mdx"], {
cwd: path.join(process.cwd(), "pages/blog"),
});
let articles = await Promise.all(articleFilenames.map(importArticle));
return articles.sort((a, z) => new Date(z.date).valueOf() - new Date(a.date).valueOf());
}

View File

@@ -0,0 +1,79 @@
/*!
* Sanitize an HTML string
* (c) 2021 Chris Ferdinandi, MIT License, https://gomakethings.com
* @param {String} str The HTML string to sanitize
* @return {String} The sanitized string
*/
export function cleanHtml(str: string): string {
/**
* Convert the string to an HTML document
* @return {Node} An HTML document
*/
function stringToHTML() {
let parser = new DOMParser();
let doc = parser.parseFromString(str, "text/html");
return doc.body || document.createElement("body");
}
/**
* Remove <script> elements
* @param {Node} html The HTML
*/
function removeScripts(html) {
let scripts = html.querySelectorAll("script");
for (let script of scripts) {
script.remove();
}
}
/**
* Check if the attribute is potentially dangerous
* @param {String} name The attribute name
* @param {String} value The attribute value
* @return {Boolean} If true, the attribute is potentially dangerous
*/
function isPossiblyDangerous(name, value) {
let val = value.replace(/\s+/g, "").toLowerCase();
if (["src", "href", "xlink:href"].includes(name)) {
if (val.includes("javascript:") || val.includes("data:")) return true;
}
if (name.startsWith("on")) return true;
}
/**
* Remove potentially dangerous attributes from an element
* @param {Node} elem The element
*/
function removeAttributes(elem) {
// Loop through each attribute
// If it's dangerous, remove it
let atts = elem.attributes;
for (let { name, value } of atts) {
if (!isPossiblyDangerous(name, value)) continue;
elem.removeAttribute(name);
}
}
/**
* Remove dangerous stuff from the HTML document's nodes
* @param {Node} html The HTML document
*/
function clean(html) {
let nodes = html.children;
for (let node of nodes) {
removeAttributes(node);
clean(node);
}
}
// Convert the string to HTML
let html = stringToHTML();
// Sanitize it
removeScripts(html);
clean(html);
// If the user wants HTML nodes back, return them
// Otherwise, pass a sanitized string back
return html.innerHTML;
}

View File

@@ -0,0 +1,70 @@
export const handleFeedbackSubmit = async (YesNo, pageUrl) => {
const response_data = {
data: {
isHelpful: YesNo,
pageUrl: pageUrl,
},
};
const payload = {
response: response_data,
surveyId: process.env.NEXT_PUBLIC_FORMBRICKS_COM_DOCS_FEEDBACK_SURVEY_ID,
};
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_FORMBRICKS_COM_API_HOST}/api/v1/client/environments/${process.env.NEXT_PUBLIC_FORMBRICKS_COM_ENVIRONMENT_ID}/responses`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);
if (res.ok) {
const responseJson = await res.json();
return responseJson.id; // Return the response ID
} else {
console.error("Error submitting form");
}
} catch (error) {
console.error("Error submitting form:", error);
}
};
export const updateFeedback = async (freeText, responseId) => {
if (!responseId) {
console.error("No response ID available");
return;
}
const payload = {
response: {
data: {
additionalInfo: freeText,
},
finished: true,
},
};
try {
const res = await fetch(
`${process.env.NEXT_PUBLIC_FORMBRICKS_COM_API_HOST}/api/v1/client/environments/${process.env.NEXT_PUBLIC_FORMBRICKS_COM_ENVIRONMENT_ID}/responses/${responseId}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
}
);
if (!res.ok) {
console.error("Error updating response");
}
} catch (error) {
console.error("Error updating response:", error);
}
};

View File

@@ -0,0 +1,8 @@
export function formatDate(dateString: string) {
return new Date(`${dateString}T00:00:00Z`).toLocaleDateString("en-US", {
day: "numeric",
month: "long",
year: "numeric",
timeZone: "UTC",
});
}