index.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. import { v4 as uuidv4 } from 'uuid';
  2. import sha256 from 'js-sha256';
  3. import { WEBUI_BASE_URL } from '$lib/constants';
  4. import { scrollPaginationEnabled, chats, currentChatPage } from '$lib/stores';
  5. import { getChatList } from '$lib/apis/chats';
  6. //////////////////////////
  7. // Helper functions
  8. //////////////////////////
  9. const convertLatexToSingleLine = (content) => {
  10. // Patterns to match multiline LaTeX blocks
  11. const patterns = [
  12. /(\$\$[\s\S]*?\$\$)/g, // Match $$ ... $$
  13. /(\\\[[\s\S]*?\\\])/g, // Match \[ ... \]
  14. /(\\begin\{[a-z]+\}[\s\S]*?\\end\{[a-z]+\})/g // Match \begin{...} ... \end{...}
  15. ];
  16. patterns.forEach((pattern) => {
  17. content = content.replace(pattern, (match) => {
  18. return match.replace(/\s*\n\s*/g, ' ').trim();
  19. });
  20. });
  21. return content;
  22. };
  23. export const sanitizeResponseContent = (content: string) => {
  24. // replace single backslash with double backslash
  25. content = content.replace(/\\/g, '\\\\');
  26. content = convertLatexToSingleLine(content);
  27. // First, temporarily replace valid <video> tags with a placeholder
  28. const videoTagRegex = /<video\s+src="([^"]+)"\s+controls><\/video>/gi;
  29. const placeholders: string[] = [];
  30. content = content.replace(videoTagRegex, (_, src) => {
  31. const placeholder = `{{VIDEO_${placeholders.length}}}`;
  32. placeholders.push(`<video src="${src}" controls></video>`);
  33. return placeholder;
  34. });
  35. // Now apply the sanitization to the rest of the content
  36. content = content
  37. .replace(/<\|[a-z]*$/, '')
  38. .replace(/<\|[a-z]+\|$/, '')
  39. .replace(/<$/, '')
  40. .replaceAll(/<\|[a-z]+\|>/g, ' ')
  41. .replaceAll('<', '&lt;')
  42. .replaceAll('>', '&gt;')
  43. .trim();
  44. // Replace placeholders with original <video> tags
  45. placeholders.forEach((placeholder, index) => {
  46. content = content.replace(`{{VIDEO_${index}}}`, placeholder);
  47. });
  48. return content.trim();
  49. };
  50. export const replaceTokens = (content, char, user) => {
  51. const charToken = /{{char}}/gi;
  52. const userToken = /{{user}}/gi;
  53. const videoIdToken = /{{VIDEO_FILE_ID_([a-f0-9-]+)}}/gi; // Regex to capture the video ID
  54. const htmlIdToken = /{{HTML_FILE_ID_([a-f0-9-]+)}}/gi; // Regex to capture the HTML ID
  55. // Replace {{char}} if char is provided
  56. if (char !== undefined && char !== null) {
  57. content = content.replace(charToken, char);
  58. }
  59. // Replace {{user}} if user is provided
  60. if (user !== undefined && user !== null) {
  61. content = content.replace(userToken, user);
  62. }
  63. // Replace video ID tags with corresponding <video> elements
  64. content = content.replace(videoIdToken, (match, fileId) => {
  65. const videoUrl = `${WEBUI_BASE_URL}/api/v1/files/${fileId}/content`;
  66. return `<video src="${videoUrl}" controls></video>`;
  67. });
  68. // Replace HTML ID tags with corresponding HTML content
  69. content = content.replace(htmlIdToken, (match, fileId) => {
  70. const htmlUrl = `${WEBUI_BASE_URL}/api/v1/files/${fileId}/content`;
  71. return `<iframe src="${htmlUrl}" width="100%" frameborder="0" onload="this.style.height=(this.contentWindow.document.body.scrollHeight+20)+'px';"></iframe>`;
  72. });
  73. return content;
  74. };
  75. export const revertSanitizedResponseContent = (content: string) => {
  76. return content.replaceAll('&lt;', '<').replaceAll('&gt;', '>');
  77. };
  78. export const capitalizeFirstLetter = (string) => {
  79. return string.charAt(0).toUpperCase() + string.slice(1);
  80. };
  81. export const splitStream = (splitOn) => {
  82. let buffer = '';
  83. return new TransformStream({
  84. transform(chunk, controller) {
  85. buffer += chunk;
  86. const parts = buffer.split(splitOn);
  87. parts.slice(0, -1).forEach((part) => controller.enqueue(part));
  88. buffer = parts[parts.length - 1];
  89. },
  90. flush(controller) {
  91. if (buffer) controller.enqueue(buffer);
  92. }
  93. });
  94. };
  95. export const convertMessagesToHistory = (messages) => {
  96. const history = {
  97. messages: {},
  98. currentId: null
  99. };
  100. let parentMessageId = null;
  101. let messageId = null;
  102. for (const message of messages) {
  103. messageId = uuidv4();
  104. if (parentMessageId !== null) {
  105. history.messages[parentMessageId].childrenIds = [
  106. ...history.messages[parentMessageId].childrenIds,
  107. messageId
  108. ];
  109. }
  110. history.messages[messageId] = {
  111. ...message,
  112. id: messageId,
  113. parentId: parentMessageId,
  114. childrenIds: []
  115. };
  116. parentMessageId = messageId;
  117. }
  118. history.currentId = messageId;
  119. return history;
  120. };
  121. export const getGravatarURL = (email) => {
  122. // Trim leading and trailing whitespace from
  123. // an email address and force all characters
  124. // to lower case
  125. const address = String(email).trim().toLowerCase();
  126. // Create a SHA256 hash of the final string
  127. const hash = sha256(address);
  128. // Grab the actual image URL
  129. return `https://www.gravatar.com/avatar/${hash}`;
  130. };
  131. export const canvasPixelTest = () => {
  132. // Test a 1x1 pixel to potentially identify browser/plugin fingerprint blocking or spoofing
  133. // Inspiration: https://github.com/kkapsner/CanvasBlocker/blob/master/test/detectionTest.js
  134. const canvas = document.createElement('canvas');
  135. const ctx = canvas.getContext('2d');
  136. canvas.height = 1;
  137. canvas.width = 1;
  138. const imageData = new ImageData(canvas.width, canvas.height);
  139. const pixelValues = imageData.data;
  140. // Generate RGB test data
  141. for (let i = 0; i < imageData.data.length; i += 1) {
  142. if (i % 4 !== 3) {
  143. pixelValues[i] = Math.floor(256 * Math.random());
  144. } else {
  145. pixelValues[i] = 255;
  146. }
  147. }
  148. ctx.putImageData(imageData, 0, 0);
  149. const p = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
  150. // Read RGB data and fail if unmatched
  151. for (let i = 0; i < p.length; i += 1) {
  152. if (p[i] !== pixelValues[i]) {
  153. console.log(
  154. 'canvasPixelTest: Wrong canvas pixel RGB value detected:',
  155. p[i],
  156. 'at:',
  157. i,
  158. 'expected:',
  159. pixelValues[i]
  160. );
  161. console.log('canvasPixelTest: Canvas blocking or spoofing is likely');
  162. return false;
  163. }
  164. }
  165. return true;
  166. };
  167. export const generateInitialsImage = (name) => {
  168. const canvas = document.createElement('canvas');
  169. const ctx = canvas.getContext('2d');
  170. canvas.width = 100;
  171. canvas.height = 100;
  172. if (!canvasPixelTest()) {
  173. console.log(
  174. 'generateInitialsImage: failed pixel test, fingerprint evasion is likely. Using default image.'
  175. );
  176. return '/user.png';
  177. }
  178. ctx.fillStyle = '#F39C12';
  179. ctx.fillRect(0, 0, canvas.width, canvas.height);
  180. ctx.fillStyle = '#FFFFFF';
  181. ctx.font = '40px Helvetica';
  182. ctx.textAlign = 'center';
  183. ctx.textBaseline = 'middle';
  184. const sanitizedName = name.trim();
  185. const initials =
  186. sanitizedName.length > 0
  187. ? sanitizedName[0] +
  188. (sanitizedName.split(' ').length > 1
  189. ? sanitizedName[sanitizedName.lastIndexOf(' ') + 1]
  190. : '')
  191. : '';
  192. ctx.fillText(initials.toUpperCase(), canvas.width / 2, canvas.height / 2);
  193. return canvas.toDataURL();
  194. };
  195. export const copyToClipboard = async (text) => {
  196. let result = false;
  197. if (!navigator.clipboard) {
  198. const textArea = document.createElement('textarea');
  199. textArea.value = text;
  200. // Avoid scrolling to bottom
  201. textArea.style.top = '0';
  202. textArea.style.left = '0';
  203. textArea.style.position = 'fixed';
  204. document.body.appendChild(textArea);
  205. textArea.focus();
  206. textArea.select();
  207. try {
  208. const successful = document.execCommand('copy');
  209. const msg = successful ? 'successful' : 'unsuccessful';
  210. console.log('Fallback: Copying text command was ' + msg);
  211. result = true;
  212. } catch (err) {
  213. console.error('Fallback: Oops, unable to copy', err);
  214. }
  215. document.body.removeChild(textArea);
  216. return result;
  217. }
  218. result = await navigator.clipboard
  219. .writeText(text)
  220. .then(() => {
  221. console.log('Async: Copying to clipboard was successful!');
  222. return true;
  223. })
  224. .catch((error) => {
  225. console.error('Async: Could not copy text: ', error);
  226. return false;
  227. });
  228. return result;
  229. };
  230. export const compareVersion = (latest, current) => {
  231. return current === '0.0.0'
  232. ? false
  233. : current.localeCompare(latest, undefined, {
  234. numeric: true,
  235. sensitivity: 'case',
  236. caseFirst: 'upper'
  237. }) < 0;
  238. };
  239. export const findWordIndices = (text) => {
  240. const regex = /\[([^\]]+)\]/g;
  241. const matches = [];
  242. let match;
  243. while ((match = regex.exec(text)) !== null) {
  244. matches.push({
  245. word: match[1],
  246. startIndex: match.index,
  247. endIndex: regex.lastIndex - 1
  248. });
  249. }
  250. return matches;
  251. };
  252. export const removeFirstHashWord = (inputString) => {
  253. // Split the string into an array of words
  254. const words = inputString.split(' ');
  255. // Find the index of the first word that starts with #
  256. const index = words.findIndex((word) => word.startsWith('#'));
  257. // Remove the first word with #
  258. if (index !== -1) {
  259. words.splice(index, 1);
  260. }
  261. // Join the remaining words back into a string
  262. const resultString = words.join(' ');
  263. return resultString;
  264. };
  265. export const transformFileName = (fileName) => {
  266. // Convert to lowercase
  267. const lowerCaseFileName = fileName.toLowerCase();
  268. // Remove special characters using regular expression
  269. const sanitizedFileName = lowerCaseFileName.replace(/[^\w\s]/g, '');
  270. // Replace spaces with dashes
  271. const finalFileName = sanitizedFileName.replace(/\s+/g, '-');
  272. return finalFileName;
  273. };
  274. export const calculateSHA256 = async (file) => {
  275. // Create a FileReader to read the file asynchronously
  276. const reader = new FileReader();
  277. // Define a promise to handle the file reading
  278. const readFile = new Promise((resolve, reject) => {
  279. reader.onload = () => resolve(reader.result);
  280. reader.onerror = reject;
  281. });
  282. // Read the file as an ArrayBuffer
  283. reader.readAsArrayBuffer(file);
  284. try {
  285. // Wait for the FileReader to finish reading the file
  286. const buffer = await readFile;
  287. // Convert the ArrayBuffer to a Uint8Array
  288. const uint8Array = new Uint8Array(buffer);
  289. // Calculate the SHA-256 hash using Web Crypto API
  290. const hashBuffer = await crypto.subtle.digest('SHA-256', uint8Array);
  291. // Convert the hash to a hexadecimal string
  292. const hashArray = Array.from(new Uint8Array(hashBuffer));
  293. const hashHex = hashArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
  294. return `${hashHex}`;
  295. } catch (error) {
  296. console.error('Error calculating SHA-256 hash:', error);
  297. throw error;
  298. }
  299. };
  300. export const getImportOrigin = (_chats) => {
  301. // Check what external service chat imports are from
  302. if ('mapping' in _chats[0]) {
  303. return 'openai';
  304. }
  305. return 'webui';
  306. };
  307. export const getUserPosition = async (raw = false) => {
  308. // Get the user's location using the Geolocation API
  309. const position = await new Promise((resolve, reject) => {
  310. navigator.geolocation.getCurrentPosition(resolve, reject);
  311. }).catch((error) => {
  312. console.error('Error getting user location:', error);
  313. throw error;
  314. });
  315. if (!position) {
  316. return 'Location not available';
  317. }
  318. // Extract the latitude and longitude from the position
  319. const { latitude, longitude } = position.coords;
  320. if (raw) {
  321. return { latitude, longitude };
  322. } else {
  323. return `${latitude.toFixed(3)}, ${longitude.toFixed(3)} (lat, long)`;
  324. }
  325. };
  326. const convertOpenAIMessages = (convo) => {
  327. // Parse OpenAI chat messages and create chat dictionary for creating new chats
  328. const mapping = convo['mapping'];
  329. const messages = [];
  330. let currentId = '';
  331. let lastId = null;
  332. for (let message_id in mapping) {
  333. const message = mapping[message_id];
  334. currentId = message_id;
  335. try {
  336. if (
  337. messages.length == 0 &&
  338. (message['message'] == null ||
  339. (message['message']['content']['parts']?.[0] == '' &&
  340. message['message']['content']['text'] == null))
  341. ) {
  342. // Skip chat messages with no content
  343. continue;
  344. } else {
  345. const new_chat = {
  346. id: message_id,
  347. parentId: lastId,
  348. childrenIds: message['children'] || [],
  349. role: message['message']?.['author']?.['role'] !== 'user' ? 'assistant' : 'user',
  350. content:
  351. message['message']?.['content']?.['parts']?.[0] ||
  352. message['message']?.['content']?.['text'] ||
  353. '',
  354. model: 'gpt-3.5-turbo',
  355. done: true,
  356. context: null
  357. };
  358. messages.push(new_chat);
  359. lastId = currentId;
  360. }
  361. } catch (error) {
  362. console.log('Error with', message, '\nError:', error);
  363. }
  364. }
  365. let history = {};
  366. messages.forEach((obj) => (history[obj.id] = obj));
  367. const chat = {
  368. history: {
  369. currentId: currentId,
  370. messages: history // Need to convert this to not a list and instead a json object
  371. },
  372. models: ['gpt-3.5-turbo'],
  373. messages: messages,
  374. options: {},
  375. timestamp: convo['create_time'],
  376. title: convo['title'] ?? 'New Chat'
  377. };
  378. return chat;
  379. };
  380. const validateChat = (chat) => {
  381. // Because ChatGPT sometimes has features we can't use like DALL-E or migh have corrupted messages, need to validate
  382. const messages = chat.messages;
  383. // Check if messages array is empty
  384. if (messages.length === 0) {
  385. return false;
  386. }
  387. // Last message's children should be an empty array
  388. const lastMessage = messages[messages.length - 1];
  389. if (lastMessage.childrenIds.length !== 0) {
  390. return false;
  391. }
  392. // First message's parent should be null
  393. const firstMessage = messages[0];
  394. if (firstMessage.parentId !== null) {
  395. return false;
  396. }
  397. // Every message's content should be a string
  398. for (let message of messages) {
  399. if (typeof message.content !== 'string') {
  400. return false;
  401. }
  402. }
  403. return true;
  404. };
  405. export const convertOpenAIChats = (_chats) => {
  406. // Create a list of dictionaries with each conversation from import
  407. const chats = [];
  408. let failed = 0;
  409. for (let convo of _chats) {
  410. const chat = convertOpenAIMessages(convo);
  411. if (validateChat(chat)) {
  412. chats.push({
  413. id: convo['id'],
  414. user_id: '',
  415. title: convo['title'],
  416. chat: chat,
  417. timestamp: convo['timestamp']
  418. });
  419. } else {
  420. failed++;
  421. }
  422. }
  423. console.log(failed, 'Conversations could not be imported');
  424. return chats;
  425. };
  426. export const isValidHttpUrl = (string) => {
  427. let url;
  428. try {
  429. url = new URL(string);
  430. } catch (_) {
  431. return false;
  432. }
  433. return url.protocol === 'http:' || url.protocol === 'https:';
  434. };
  435. export const removeEmojis = (str) => {
  436. // Regular expression to match emojis
  437. const emojiRegex = /[\uD800-\uDBFF][\uDC00-\uDFFF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g;
  438. // Replace emojis with an empty string
  439. return str.replace(emojiRegex, '');
  440. };
  441. export const removeFormattings = (str) => {
  442. return str.replace(/(\*)(.*?)\1/g, '').replace(/(```)(.*?)\1/gs, '');
  443. };
  444. export const extractSentences = (text) => {
  445. // This regular expression matches code blocks marked by triple backticks
  446. const codeBlockRegex = /```[\s\S]*?```/g;
  447. let codeBlocks = [];
  448. let index = 0;
  449. // Temporarily replace code blocks with placeholders and store the blocks separately
  450. text = text.replace(codeBlockRegex, (match) => {
  451. let placeholder = `\u0000${index}\u0000`; // Use a unique placeholder
  452. codeBlocks[index++] = match;
  453. return placeholder;
  454. });
  455. // Split the modified text into sentences based on common punctuation marks, avoiding these blocks
  456. let sentences = text.split(/(?<=[.!?])\s+/);
  457. // Restore code blocks and process sentences
  458. sentences = sentences.map((sentence) => {
  459. // Check if the sentence includes a placeholder for a code block
  460. return sentence.replace(/\u0000(\d+)\u0000/g, (_, idx) => codeBlocks[idx]);
  461. });
  462. return sentences
  463. .map((sentence) => removeFormattings(removeEmojis(sentence.trim())))
  464. .filter((sentence) => sentence);
  465. };
  466. export const extractSentencesForAudio = (text) => {
  467. return extractSentences(text).reduce((mergedTexts, currentText) => {
  468. const lastIndex = mergedTexts.length - 1;
  469. if (lastIndex >= 0) {
  470. const previousText = mergedTexts[lastIndex];
  471. const wordCount = previousText.split(/\s+/).length;
  472. if (wordCount < 2) {
  473. mergedTexts[lastIndex] = previousText + ' ' + currentText;
  474. } else {
  475. mergedTexts.push(currentText);
  476. }
  477. } else {
  478. mergedTexts.push(currentText);
  479. }
  480. return mergedTexts;
  481. }, []);
  482. };
  483. export const blobToFile = (blob, fileName) => {
  484. // Create a new File object from the Blob
  485. const file = new File([blob], fileName, { type: blob.type });
  486. return file;
  487. };
  488. /**
  489. * @param {string} template - The template string containing placeholders.
  490. * @returns {string} The template string with the placeholders replaced by the prompt.
  491. */
  492. export const promptTemplate = (
  493. template: string,
  494. user_name?: string,
  495. user_location?: string
  496. ): string => {
  497. // Get the current date
  498. const currentDate = new Date();
  499. // Format the date to YYYY-MM-DD
  500. const formattedDate =
  501. currentDate.getFullYear() +
  502. '-' +
  503. String(currentDate.getMonth() + 1).padStart(2, '0') +
  504. '-' +
  505. String(currentDate.getDate()).padStart(2, '0');
  506. // Format the time to HH:MM:SS AM/PM
  507. const currentTime = currentDate.toLocaleTimeString('en-US', {
  508. hour: 'numeric',
  509. minute: 'numeric',
  510. second: 'numeric',
  511. hour12: true
  512. });
  513. // Replace {{CURRENT_DATETIME}} in the template with the formatted datetime
  514. template = template.replace('{{CURRENT_DATETIME}}', `${formattedDate} ${currentTime}`);
  515. // Replace {{CURRENT_DATE}} in the template with the formatted date
  516. template = template.replace('{{CURRENT_DATE}}', formattedDate);
  517. // Replace {{CURRENT_TIME}} in the template with the formatted time
  518. template = template.replace('{{CURRENT_TIME}}', currentTime);
  519. if (user_name) {
  520. // Replace {{USER_NAME}} in the template with the user's name
  521. template = template.replace('{{USER_NAME}}', user_name);
  522. }
  523. if (user_location) {
  524. // Replace {{USER_LOCATION}} in the template with the current location
  525. template = template.replace('{{USER_LOCATION}}', user_location);
  526. }
  527. return template;
  528. };
  529. /**
  530. * This function is used to replace placeholders in a template string with the provided prompt.
  531. * The placeholders can be in the following formats:
  532. * - `{{prompt}}`: This will be replaced with the entire prompt.
  533. * - `{{prompt:start:<length>}}`: This will be replaced with the first <length> characters of the prompt.
  534. * - `{{prompt:end:<length>}}`: This will be replaced with the last <length> characters of the prompt.
  535. * - `{{prompt:middletruncate:<length>}}`: This will be replaced with the prompt truncated to <length> characters, with '...' in the middle.
  536. *
  537. * @param {string} template - The template string containing placeholders.
  538. * @param {string} prompt - The string to replace the placeholders with.
  539. * @returns {string} The template string with the placeholders replaced by the prompt.
  540. */
  541. export const titleGenerationTemplate = (template: string, prompt: string): string => {
  542. template = template.replace(
  543. /{{prompt}}|{{prompt:start:(\d+)}}|{{prompt:end:(\d+)}}|{{prompt:middletruncate:(\d+)}}/g,
  544. (match, startLength, endLength, middleLength) => {
  545. if (match === '{{prompt}}') {
  546. return prompt;
  547. } else if (match.startsWith('{{prompt:start:')) {
  548. return prompt.substring(0, startLength);
  549. } else if (match.startsWith('{{prompt:end:')) {
  550. return prompt.slice(-endLength);
  551. } else if (match.startsWith('{{prompt:middletruncate:')) {
  552. if (prompt.length <= middleLength) {
  553. return prompt;
  554. }
  555. const start = prompt.slice(0, Math.ceil(middleLength / 2));
  556. const end = prompt.slice(-Math.floor(middleLength / 2));
  557. return `${start}...${end}`;
  558. }
  559. return '';
  560. }
  561. );
  562. template = promptTemplate(template);
  563. return template;
  564. };
  565. export const approximateToHumanReadable = (nanoseconds: number) => {
  566. const seconds = Math.floor((nanoseconds / 1e9) % 60);
  567. const minutes = Math.floor((nanoseconds / 6e10) % 60);
  568. const hours = Math.floor((nanoseconds / 3.6e12) % 24);
  569. const results: string[] = [];
  570. if (seconds >= 0) {
  571. results.push(`${seconds}s`);
  572. }
  573. if (minutes > 0) {
  574. results.push(`${minutes}m`);
  575. }
  576. if (hours > 0) {
  577. results.push(`${hours}h`);
  578. }
  579. return results.reverse().join(' ');
  580. };
  581. export const getTimeRange = (timestamp) => {
  582. const now = new Date();
  583. const date = new Date(timestamp * 1000); // Convert Unix timestamp to milliseconds
  584. // Calculate the difference in milliseconds
  585. const diffTime = now.getTime() - date.getTime();
  586. const diffDays = diffTime / (1000 * 3600 * 24);
  587. const nowDate = now.getDate();
  588. const nowMonth = now.getMonth();
  589. const nowYear = now.getFullYear();
  590. const dateDate = date.getDate();
  591. const dateMonth = date.getMonth();
  592. const dateYear = date.getFullYear();
  593. if (nowYear === dateYear && nowMonth === dateMonth && nowDate === dateDate) {
  594. return 'Today';
  595. } else if (nowYear === dateYear && nowMonth === dateMonth && nowDate - dateDate === 1) {
  596. return 'Yesterday';
  597. } else if (diffDays <= 7) {
  598. return 'Previous 7 days';
  599. } else if (diffDays <= 30) {
  600. return 'Previous 30 days';
  601. } else if (nowYear === dateYear) {
  602. return date.toLocaleString('default', { month: 'long' });
  603. } else {
  604. return date.getFullYear().toString();
  605. }
  606. };
  607. /**
  608. * Extract frontmatter as a dictionary from the specified content string.
  609. * @param content {string} - The content string with potential frontmatter.
  610. * @returns {Object} - The extracted frontmatter as a dictionary.
  611. */
  612. export const extractFrontmatter = (content) => {
  613. const frontmatter = {};
  614. let frontmatterStarted = false;
  615. let frontmatterEnded = false;
  616. const frontmatterPattern = /^\s*([a-z_]+):\s*(.*)\s*$/i;
  617. // Split content into lines
  618. const lines = content.split('\n');
  619. // Check if the content starts with triple quotes
  620. if (lines[0].trim() !== '"""') {
  621. return {};
  622. }
  623. frontmatterStarted = true;
  624. for (let i = 1; i < lines.length; i++) {
  625. const line = lines[i];
  626. if (line.includes('"""')) {
  627. if (frontmatterStarted) {
  628. frontmatterEnded = true;
  629. break;
  630. }
  631. }
  632. if (frontmatterStarted && !frontmatterEnded) {
  633. const match = frontmatterPattern.exec(line);
  634. if (match) {
  635. const [, key, value] = match;
  636. frontmatter[key.trim()] = value.trim();
  637. }
  638. }
  639. }
  640. return frontmatter;
  641. };
  642. // Function to determine the best matching language
  643. export const bestMatchingLanguage = (supportedLanguages, preferredLanguages, defaultLocale) => {
  644. const languages = supportedLanguages.map((lang) => lang.code);
  645. const match = preferredLanguages
  646. .map((prefLang) => languages.find((lang) => lang.startsWith(prefLang)))
  647. .find(Boolean);
  648. console.log(languages, preferredLanguages, match, defaultLocale);
  649. return match || defaultLocale;
  650. };