From 8dac2a2140b5897e667e923b873ffcc6994852ed Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 26 Jun 2024 10:22:31 -0700 Subject: [PATCH] refac --- backend/config.py | 7 +++++-- src/lib/utils/index.ts | 43 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/backend/config.py b/backend/config.py index 4af3ae281..3a825f53a 100644 --- a/backend/config.py +++ b/backend/config.py @@ -569,12 +569,15 @@ OLLAMA_API_BASE_URL = os.environ.get( ) OLLAMA_BASE_URL = os.environ.get("OLLAMA_BASE_URL", "") -AIOHTTP_CLIENT_TIMEOUT = os.environ.get("AIOHTTP_CLIENT_TIMEOUT", "300") +AIOHTTP_CLIENT_TIMEOUT = os.environ.get("AIOHTTP_CLIENT_TIMEOUT", "") if AIOHTTP_CLIENT_TIMEOUT == "": AIOHTTP_CLIENT_TIMEOUT = None else: - AIOHTTP_CLIENT_TIMEOUT = int(AIOHTTP_CLIENT_TIMEOUT) + try: + AIOHTTP_CLIENT_TIMEOUT = int(AIOHTTP_CLIENT_TIMEOUT) + except: + AIOHTTP_CLIENT_TIMEOUT = 300 K8S_FLAG = os.environ.get("K8S_FLAG", "") diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index c169a78b1..6e9f29205 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -703,3 +703,46 @@ export const getTimeRange = (timestamp) => { return date.getFullYear().toString(); } }; + +/** + * Extract frontmatter as a dictionary from the specified content string. + * @param content {string} - The content string with potential frontmatter. + * @returns {Object} - The extracted frontmatter as a dictionary. + */ +export const extractFrontmatter = (content) => { + const frontmatter = {}; + let frontmatterStarted = false; + let frontmatterEnded = false; + const frontmatterPattern = /^\s*([a-z_]+):\s*(.*)\s*$/i; + + // Split content into lines + const lines = content.split('\n'); + + // Check if the content starts with triple quotes + if (lines[0].trim() !== '"""') { + return {}; + } + + frontmatterStarted = true; + + for (let i = 1; i < lines.length; i++) { + const line = lines[i]; + + if (line.includes('"""')) { + if (frontmatterStarted) { + frontmatterEnded = true; + break; + } + } + + if (frontmatterStarted && !frontmatterEnded) { + const match = frontmatterPattern.exec(line); + if (match) { + const [, key, value] = match; + frontmatter[key.trim()] = value.trim(); + } + } + } + + return frontmatter; +};