diff --git a/package.json b/package.json index 7dd14a2..1153891 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dayjs": "^1.11.12", "lru-cache": "^11.0.0", "ofetch": "^1.3.4", + "prismjs": "^1.29.0", "sanitize-html": "^2.13.0" }, "devDependencies": { diff --git a/src/layouts/base.astro b/src/layouts/base.astro index f434601..50c626f 100644 --- a/src/layouts/base.astro +++ b/src/layouts/base.astro @@ -51,12 +51,8 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT') - + + - +
@@ -112,12 +97,7 @@ const FOOTER_INJECT = getEnv(import.meta.env, Astro, 'FOOTER_INJECT') > BroadcastChannel & - + Sepia diff --git a/src/lib/telegram/index.js b/src/lib/telegram/index.js index 603a413..f9a4ac4 100644 --- a/src/lib/telegram/index.js +++ b/src/lib/telegram/index.js @@ -1,8 +1,12 @@ import { $fetch } from 'ofetch' import * as cheerio from 'cheerio' import { LRUCache } from 'lru-cache' +import Prism from 'prismjs' +import loadLanguages from 'prismjs/components/' import { getEnv } from '../env' +loadLanguages(['javascript', 'python', 'css', 'html']) + const cache = new LRUCache({ ttl: 1000 * 60 * 5, // 5 minutes maxSize: 50 * 1024 * 1024, // 50MB @@ -108,9 +112,33 @@ function modifyHTMLContent($, content, { index } = {}) { ?.wrap('') ?.before(``) }) + $(content).find('pre').each((_index, pre) => { + const code = $(pre).text() + const language = detectLanguage(code) // You'll need to implement this function + const highlightedCode = Prism.highlight(code, Prism.languages[language], language) + $(pre).html(`${highlightedCode}`) + }) return content } +function detectLanguage(code) { + // Implement a simple language detection logic + // This is a basic example and might need refinement + if (code.includes('function') || code.includes('const') || code.includes('let')) { + return 'javascript' + } + else if (code.includes('def ') || code.includes('import ')) { + return 'python' + } + else if (code.includes('') || code.includes('')) { + return 'html' + } + else if (code.includes('{') && code.includes('}') && code.includes(':')) { + return 'css' + } + return 'clike' // default to C-like syntax +} + function getPost($, item, { channel, staticProxy, index = 0 }) { item = item ? $(item).find('.tgme_widget_message') : $('.tgme_widget_message') const content = $(item).find('.js-message_reply_text')?.length > 0