From 346bd1cd53e1bd3bd881f94552efadeebab430e4 Mon Sep 17 00:00:00 2001 From: Josh Hubbard Date: Wed, 6 Dec 2023 09:15:53 +0100 Subject: [PATCH] feat(frontend-search): add `I am lucky` option --- plugins/frontend-search/README.md | 11 ++++++++++- plugins/frontend-search/frontend-search.plugin.zsh | 14 ++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/plugins/frontend-search/README.md b/plugins/frontend-search/README.md index 619d31e7f..920795060 100644 --- a/plugins/frontend-search/README.md +++ b/plugins/frontend-search/README.md @@ -66,7 +66,16 @@ If you want to have another context, open an Issue and tell us! ## Fallback search behaviour -The plugin will use Google as a fallback if the docs site for a search context does not have a search function. You can set the fallback search engine to DuckDuckGo by setting `FRONTEND_SEARCH_FALLBACK='duckduckgo'` in your `~/.zshrc` file before Oh My Zsh is sourced. +The plugin will use Google as a fallback if the docs site for a search context does not have a search +function. You can set the fallback search engine to DuckDuckGo by setting +`FRONTEND_SEARCH_FALLBACK='duckduckgo'` in your `~/.zshrc` file before Oh My Zsh is sourced. + +## DuckDuckGo Lucky Search + +Enable DuckDuckGo's "ducky" (lucky) search feature to automatically access the top search result. This feature +is optimized for DuckDuckGo, as Google redirects to an intermediate page. The FRONTEND_SEARCH_FALLBACK_LUCKY +environment variable triggers the use of DuckDuckGo's lucky search, rendering the FRONTEND_SEARCH_FALLBACK +setting unnecessary in this context. ## Author diff --git a/plugins/frontend-search/frontend-search.plugin.zsh b/plugins/frontend-search/frontend-search.plugin.zsh index 541b23701..c96596eb9 100644 --- a/plugins/frontend-search/frontend-search.plugin.zsh +++ b/plugins/frontend-search/frontend-search.plugin.zsh @@ -30,10 +30,16 @@ alias vuejs='frontend vuejs' alias nextjs='frontend nextjs' function _frontend_fallback() { - case "$FRONTEND_SEARCH_FALLBACK" in - duckduckgo) echo "https://duckduckgo.com/?sites=$1&q=" ;; - *) echo "https://google.com/search?as_sitesearch=$1&as_q=" ;; - esac + if [[ "$FRONTEND_SEARCH_FALLBACK_LUCKY" == "true" ]]; then + case true in + *) echo "https://duckduckgo.com/?q=!ducky+site%3A$1+" ;; + esac + else + case "$FRONTEND_SEARCH_FALLBACK" in + duckduckgo) echo "https://duckduckgo.com/?sites=$1&q=" ;; + *) echo "https://google.com/search?as_sitesearch=$1&as_q=" ;; + esac + fi } function frontend() {