[{"data":1,"prerenderedAt":310},["ShallowReactive",2],{"content:\u002F2024\u002Fblog-link-pattern":3,"surround:\u002F2024\u002Fblog-link-pattern":298},{"id":4,"title":5,"body":6,"categories":265,"date":267,"description":268,"draft":269,"extension":270,"image":271,"meta":272,"navigation":274,"path":275,"permalink":271,"published":271,"readingTime":276,"recommend":271,"references":281,"seo":288,"sitemap":289,"stem":290,"tags":291,"type":295,"updated":296,"__hash__":297},"content\u002Fposts\u002F2024\u002Fblog-link-pattern.md","把博客文章按年份放在子文件夹中了",{"type":7,"value":8,"toc":255},"minimark",[9,19,24,43,46,62,72,75,93,96,99,108,115,118,127,130,133,140,150,161,170,185,191,194,217,224,233,240,246,249,252],[10,11,12,16],"blockquote",{},[13,14,15],"p",{},"我的博客很少写关于折腾博客的内容，因为我不想让博客变成博客的博客。这次的行为仅服务于博客的后期维护。",[13,17,18],{},"私以为，博客应当专注于内容，而不是囿于形式。",[20,21,23],"h2",{"id":22},"posts-目录文章过多","posts 目录文章过多",[13,25,26,27,31,32,35,36,39,40,42],{},"不知不觉，博客已经有了大约 45 篇文章，生成的文章链接以 ",[28,29,30],"code",{"code":30},":year:month\u002F"," (如 ",[28,33,34],{"code":34},"202404\u002F",") 开头，但 Markdown 源文件都放在了 ",[28,37,38],{"code":38},"_posts\u002F"," 目录下，不便于管理。因此，我想在",[28,41,38],{"code":38}," 目录里按年份创建子文件夹。",[20,44,45],{"id":45},"移动文章到年份文件夹",[13,47,48,49,54,55,61],{},"看了 ",[50,51,53],"a",{"href":52},"#references","参考资料","，博主给出了一个实用命令 ",[28,56,59],{"className":57,"code":59,"language":60},[58],"language-cmd","grep -r \"date: '2015-\" *.md -l | xargs mv -v -t 2015\u002F","cmd","，用于批量移动文件，我借助 Poe Assistant 写出了对应的 PowerShell 脚本。",[63,64,70],"pre",{"className":65,"code":67,"language":68,"meta":69},[66],"language-powershell","# 搜索匹配的文件\nGet-ChildItem -Filter \"*.md\" -Recurse | ForEach-Object {\n    $filePath = $_.FullName\n    $match = Select-String -Path $filePath -Pattern \"date: 2020-\" -Quiet\n\n    if ($match) {\n        # 获取文件名\n        $fileName = $_.Name\n\n        # 创建目标目录\n        $targetDirectory = \"2020\"\n        if (-not (Test-Path -Path $targetDirectory)) {\n            New-Item -ItemType Directory -Path $targetDirectory | Out-Null\n        }\n\n        # 移动文件\n        Move-Item -Path $filePath -Destination (Join-Path -Path $targetDirectory -ChildPath $fileName) -Verbose\n    }\n}\n","powershell","",[28,71,67],{"__ignoreMap":69},[20,73,74],{"id":74},"设置链接前缀",[13,76,77,78,81,82,85,86,88,89,92],{},"Hexo 会自动给文章链接带上子文件夹的名称，所以在 ",[28,79,80],{"code":80},"_config.yml"," 设置文章永久链接为仅 ",[28,83,84],{"code":84},":title\u002F","，就可以满足需求。如果不想要文件夹名或者想要维持原来的 URL 配置，将原先文章永久链接中的 ",[28,87,84],{"code":84}," 替换为 ",[28,90,91],{"code":91},":name\u002F"," 即可。",[20,94,95],{"id":95},"旧链接重定向",[13,97,98],{},"通过主题提供的自定义侧栏 Widget 能力，我为 404 页面引入了自定义脚本，用于重定向旧链接。",[63,100,106],{"className":101,"code":103,"filename":104,"language":105,"meta":69},[102],"language-yaml","site_tree:\n  error_page:\n    leftbar: recent, timeline, notice_404\n","_config.stellar.yml","yaml",[28,107,103],{"__ignoreMap":69},[63,109,113],{"className":110,"code":111,"filename":112,"language":105,"meta":69},[102],"notice_404:\n  layout: markdown\n  content: |\n    \u003C!-- Notice 还没想好，以后再写吧 -->\n    \u003Cscript src=\"\u002Fstatic\u002Fhandler-404.js\">\u003C\u002Fscript>\n","source\u002F_data\u002Fwidgets.yml",[28,114,111],{"__ignoreMap":69},[13,116,117],{},"用正则表达式匹配、替换，就完成了重定向。",[63,119,125],{"className":120,"code":122,"filename":123,"language":124,"meta":69},[121],"language-js","const newPath = location.pathname.match(\u002F^(\\\u002F\\d{4})\\d{2}(\\\u002F.*)\u002F)\nif (newPath)\n    location.href = newPath[1] + newPath[2]\n","static\u002Fhandler-404.js","js",[28,126,122],{"__ignoreMap":69},[13,128,129],{},"Over.",[20,131,132],{"id":132},"还没有结束",[13,134,135,139],{},[50,136,138],{"href":137},"#%E6%97%A7%E9%93%BE%E6%8E%A5%E9%87%8D%E5%AE%9A%E5%90%91","上一节"," 的内容废弃了。",[13,141,142,143,149],{},"HTTP 状态码不该是 404，所以需要通过 Netlify 提供的 ",[50,144,148],{"href":145,"rel":146},"https:\u002F\u002Fdocs.netlify.com\u002Frouting\u002Fredirects\u002Fredirect-options\u002F",[147],"nofollow","重定向能力","，让状态码变为 3xx。",[13,151,152,153,156,157,160],{},"在建博客时埋了一个坑，我的链接是 ",[28,154,155],{"code":155},":year:month\u002F:title\u002F","，而 Netlify 的 Placeholder 仅支持诸如 ",[28,158,159],{"code":159},":year\u002F:month\u002F:title\u002F"," 的形式，所以不能使用这种配置文件：",[63,162,168],{"className":163,"code":165,"filename":166,"language":167,"meta":69},[164],"language-toml","[[redirects]]\nfrom = \"\u002Fpost\u002F*\"\nto = \"\u002F:splat\"\n\n[[redirects]]\nfrom = \"\u002F:year\u002F:month\u002F:date\u002F:slug\"\nto = \"\u002F:slug\"\n","source\u002Fnetlify.toml","toml",[28,169,165],{"__ignoreMap":69},[13,171,172,173,176,177,180,181,184],{},"所以我只能给每个博客 URL 分别设置重定向规则。为了获取先前的 URL 路径，我恢复了旧的 ",[28,174,175],{"code":175},"permalink"," 配置，把其中的 ",[28,178,179],{"code":179},":title"," 改为 ",[28,182,183],{"code":183},":name","。",[63,186,189],{"className":187,"code":188,"filename":80,"language":105,"meta":69},[102],"permalink: :year:month\u002F:name\u002F\n# permalink: :title\u002F\n",[28,190,188],{"__ignoreMap":69},[13,192,193],{},"要一条一条写重定向规则，有点麻烦，可以这么做：",[195,196,197,206],"ul",{},[198,199,200,201],"li",{},"尝试用命令生成先前博客链接路径和新的博客链接路径\n",[202,203],"copy",{"code":204,"prompt":205},"hexo clean; hexo generate","PS>",[198,207,208,209,213,214],{},"findstr 不完全支持 PCRE，可以用 ",[28,210,212],{"className":211,"code":212,"language":68},[66],"Select-String \"\\\\\\d{6}\\\\\""," 代替\n",[202,215],{"code":216,"prompt":205},"dir -s .\\public\\ | findstr -r \"\\\\[0-9][0-9][0-9][0-9][0-9][0-9]\\\\\"",[13,218,219,220,223],{},"再用正则表达式稍微替换一下输出（不要忘了",[28,221,222],{"code":222},"\u002F","），就能得到 Netlify 的重定向规则文件了。",[63,225,231],{"className":226,"code":228,"filename":229,"language":230,"meta":69},[227],"language-text","……\n\u002F202304\u002Fvscode-simple-config \u002F2023\u002Fvscode-simple-config\n\u002F202305\u002Farchinstall-guide \u002F2023\u002Farchinstall-guide\n\u002F202306\u002Farchlinux-configure \u002F2023\u002Farchlinux-configure\n\u002F202307\u002Farchlinux-beautify \u002F2023\u002Farchlinux-beautify\n……\n","source\u002F_redirects","text",[28,232,228],{"__ignoreMap":69},[13,234,235,236,239],{},"如果 generate 的文件中没有 ",[28,237,238],{"code":238},"_redirects"," 文件，那就修改一下配置文件：",[63,241,244],{"className":242,"code":243,"filename":80,"language":105,"meta":69},[102],"include: [_redirects]\n",[28,245,243],{"__ignoreMap":69},[13,247,248],{},"这下应该 over 了。",[20,250,251],{"id":251},"又没有结束",[13,253,254],{},"5个月后，我通过 Google Search Console 发现收录的博客链接还是原先的。于是又通过 Nuxt 的能力编写了新的重定向脚本，并期待它能获取到 canonical 标签。",{"title":69,"searchDepth":256,"depth":256,"links":257},4,[258,260,261,262,263,264],{"id":22,"depth":259,"text":23},2,{"id":45,"depth":259,"text":45},{"id":74,"depth":259,"text":74},{"id":95,"depth":259,"text":95},{"id":132,"depth":259,"text":132},{"id":251,"depth":259,"text":251},[266],"技术","2024-04-29 11:10:18","同时更改了文章链接前缀，编写了跳转脚本，后来通过平台重定向规则实现更规范的跳转。",false,"md",null,{"slots":273},{},true,"\u002F2024\u002Fblog-link-pattern",{"text":277,"minutes":278,"time":279,"words":280},"4 min read",3.845,230700,769,[282,285],{"title":283,"link":284},"使用子文件夹管理 Hexo 文章且不改变文章永久链接","https:\u002F\u002Fprinsss.github.io\u002Fhexo-posts-in-subfolder\u002F",{"title":286,"link":287},"更改Hugo博客的永久URL链接，并用Netlify实现旧博文跳转","https:\u002F\u002Fthirdshire.com\u002Fsimplify-permalinks\u002F",{"title":5,"description":268},{"loc":275},"posts\u002F2024\u002Fblog-link-pattern",[292,293,294],"博客","路由结构","Nuxt Content","tech","2024-09-27 08:37:52","J9g7xJ-mnFYdMC8LjC6RItBLLbWvTCSyhTPDI6iY6YM",[299,304],{"title":300,"path":301,"stem":302,"date":303,"type":295,"children":-1},"写过的奇妙代码。","\u002F2024\u002Fmagic-code","posts\u002F2024\u002Fmagic-code","2024-04-07 12:32:11",{"title":305,"path":306,"stem":307,"date":308,"type":309,"children":-1},"今日有「假」","\u002F2024\u002Ffake-today","posts\u002F2024\u002Ffake-today","2024-05-03 12:22:31","story",1782091376011]