From b4a5dd021cf1dd0b5e57c276b105a77a164de2a6 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Thu, 14 Nov 2024 20:37:13 +0800 Subject: [PATCH] feat: enhance SEO settings and improve title extraction - Prioritize SEO settings from page data over environment variables - Improve title extraction logic to handle new line characters - Set SEO parameters for specific pages to improve search visibility --- src/layouts/base.astro | 6 +++--- src/lib/telegram/index.js | 2 +- src/pages/after/[cursor].astro | 4 ++++ src/pages/before/[cursor].astro | 4 ++++ src/pages/links.astro | 4 ++++ src/pages/search/[q].astro | 7 ++++++- src/pages/tags.astro | 4 ++++ 7 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/layouts/base.astro b/src/layouts/base.astro index 61f019ee..6d6c281f 100644 --- a/src/layouts/base.astro +++ b/src/layouts/base.astro @@ -22,8 +22,8 @@ const seoParams = { title: seo?.title, description: seo?.text ?? channel?.description, canonical, - noindex: getEnv(import.meta.env, Astro, 'NOINDEX'), - nofollow: getEnv(import.meta.env, Astro, 'NOFOLLOW'), + noindex: seo?.noindex ?? getEnv(import.meta.env, Astro, 'NOINDEX'), + nofollow: seo?.nofollow ?? getEnv(import.meta.env, Astro, 'NOFOLLOW'), openGraph: { basic: { type: 'website', @@ -33,7 +33,7 @@ const seoParams = { }, optional: { description: seo?.text ?? channel?.description, - locale: getEnv(import.meta.env, Astro, 'LOCALE'), + locale, }, }, extend: { diff --git a/src/lib/telegram/index.js b/src/lib/telegram/index.js index 100ec69b..10b07a3a 100644 --- a/src/lib/telegram/index.js +++ b/src/lib/telegram/index.js @@ -131,7 +131,7 @@ function getPost($, item, { channel, staticProxy, index = 0 }) { const content = $(item).find('.js-message_reply_text')?.length > 0 ? modifyHTMLContent($, $(item).find('.tgme_widget_message_text.js-message_text'), { index }) : modifyHTMLContent($, $(item).find('.tgme_widget_message_text'), { index }) - const title = content?.text()?.match(/^.*?(?=[。::]|http\S)/g)?.[0] ?? content?.text() ?? '' + const title = content?.text()?.match(/^.*?(?=[。\n]|http\S)/g)?.[0] ?? content?.text() ?? '' const id = $(item).attr('data-post')?.replace(new RegExp(`${channel}/`, 'i'), '') const tags = $(content).find('a[href^="?q="]')?.each((_index, a) => { diff --git a/src/pages/after/[cursor].astro b/src/pages/after/[cursor].astro index 5315bdce..d8e7705b 100644 --- a/src/pages/after/[cursor].astro +++ b/src/pages/after/[cursor].astro @@ -5,6 +5,10 @@ import { getChannelInfo } from '../../lib/telegram' const channel = await getChannelInfo(Astro, { after: Astro.params.cursor, }) + +channel.seo = { + noindex: true, +} --- diff --git a/src/pages/before/[cursor].astro b/src/pages/before/[cursor].astro index 242b401c..abdaa702 100644 --- a/src/pages/before/[cursor].astro +++ b/src/pages/before/[cursor].astro @@ -5,6 +5,10 @@ import { getChannelInfo } from '../../lib/telegram' const channel = await getChannelInfo(Astro, { before: Astro.params.cursor, }) + +channel.seo = { + noindex: true, +} --- diff --git a/src/pages/links.astro b/src/pages/links.astro index 29b0fa87..c24c4bbc 100644 --- a/src/pages/links.astro +++ b/src/pages/links.astro @@ -6,6 +6,10 @@ import { getEnv } from '../lib/env' const channel = await getChannelInfo(Astro) +channel.seo = { + title: 'Links', +} + const links = (getEnv(import.meta.env, Astro, 'LINKS') || '') .split(';') .filter(Boolean) diff --git a/src/pages/search/[q].astro b/src/pages/search/[q].astro index a356d941..866d6709 100644 --- a/src/pages/search/[q].astro +++ b/src/pages/search/[q].astro @@ -2,9 +2,14 @@ import List from '../../components/list.astro' import { getChannelInfo } from '../../lib/telegram' +const q = Astro.url.searchParams.get('q') || Astro.params.q const channel = await getChannelInfo(Astro, { - q: Astro.url.searchParams.get('q') || Astro.params.q, + q, }) + +channel.seo = { + title: `${q}`, +} --- diff --git a/src/pages/tags.astro b/src/pages/tags.astro index bc08ae44..651f1d05 100644 --- a/src/pages/tags.astro +++ b/src/pages/tags.astro @@ -6,6 +6,10 @@ import { getEnv } from '../lib/env' const channel = await getChannelInfo(Astro) +channel.seo = { + title: 'Tags', +} + const tags = (getEnv(import.meta.env, Astro, 'TAGS') || '').split(',') ---