import type { RecipeItem } from '~/types' import { trackCookEvent } from './analytics' /** * B 站搜索链接:按菜名搜索,结果里挑选当下最热门的教程 */ export function getRecipeSearchLink(name: string) { return `https://search.bilibili.com/all?keyword=${encodeURIComponent(name)}` } /** * 收录的原视频地址:优先使用收录的链接,否则拼出 B 站视频地址;两者都没有时返回空字符串 */ export function getRecipeVideoLink(recipe: Pick) { if (recipe.link) return recipe.link return recipe.bv ? `https://www.bilibili.com/video/${recipe.bv}` : '' } /** * 菜谱的做法链接:跳转 B 站按菜名搜索,具体收录的视频作为次级入口(见 getRecipeVideoLink) * 没有菜名时(不正常的数据)退回视频链接 */ export function getRecipeLink(recipe: Pick) { return recipe.name ? getRecipeSearchLink(recipe.name) : getRecipeVideoLink(recipe) } export function openRecipe(recipe: Pick) { const link = getRecipeLink(recipe) if (!link) return trackCookEvent('click_recipe_video') window.open(link, '_blank') } /** * 反馈这道菜的问题:打开预填好菜名和视频的 GitHub Issue 表单 */ export function getRecipeFeedbackLink(recipe: Pick) { const params = new URLSearchParams({ template: 'recipe.yml', title: `[菜谱] ${recipe.name}`, name: recipe.name, video: getRecipeVideoLink(recipe), }) return `https://github.com/YunYouJun/cook/issues/new?${params}` }