Molunerfinn / hexo-theme-melody

:musical_keyboard:A simple & beautiful & fast theme for Hexo.
https://molunerfinn.com
Other
1.59k stars 212 forks source link

文章页中使用本地搜索,会被当成相对链接访问 #327

Closed test-lin closed 3 years ago

test-lin commented 3 years ago

Melody Information

yarn Version: 1.22.4 hexo-cli Version: 4.2.0 Melody Version: 1.9.0 hexo-generator-search Version: 2.4.2

Platform: Windows 7

Browser: edge

_config.melody.yml

local_search:
  enable: true
  labels:
    input_placeholder: '文章搜索'
    hits_empty: '找不到搜索结果: ${query}'

Expected behavior

跳转到搜索结果页

http://localhost:4000/2019/04/ssl-encrypt/

Actual behavior

实际跳转到 http://localhost:4000/2019/04/algorithm-roman-to-integer/2019/04/ssl-encrypt/

Steps to reproduce the behavior

  1. 进入文章详情页
  2. 点击头部搜索进行搜索
  3. 点击搜索结果

Other

从访问错误页返回,重新打开搜索,搜索关键词还在,无搜索结果,需要删除一部分才能触发

test-lin commented 3 years ago

是 hexo-generator-search 生成的 search.xml 的问题

我在 _config.yml 里修改了 url 成 http://example.com/blog 在文章页可以正常访问搜索结果,经过几次验证,发现没有设置子目录的话 search.xml 里的 url 使用的是 相对路径。我就在搜索 js 里添加了路径开头判断。

// themes\melody\source\js\search\local-search.js:91

// show search results
if (isMatch) {
  if (! dataUrl.startsWith('/')) {
    dataUrl = '/' + dataUrl
  }

  str += '<div class="local-search__hit-item"><a href="' + dataUrl + '" class="search-result-title">' + dataTitle + '</a>' + '</div>'
  count += 1
  $('.local-search-stats__hr').show()
}

我的问题已经解决了,关闭的 issues 就交给你来关吧

Molunerfinn commented 3 years ago

感谢,最近没有时间看主题的issue。

欢迎提交一个PR,这边合入~