简体   繁体   English

Hugo - 获取视频标题并将其转换为 URL

[英]Hugo - Taking a video title and turning it into a URL

I'm working with Hugo and have a use case where there are a lot of various video titles that all follow roughly the same considerations:我正在与 Hugo 合作,并且有一个用例,其中有很多不同的视频标题都遵循大致相同的考虑:

  • remove all underscores and replace with dashes删除所有下划线并用破折号替换
  • remove all dashes with spaces on either side and replace with a dash with no spaces删除所有两边有空格的破折号,并用没有空格的破折号替换
  • remove all special characters (? and !) and replace with nothing删除所有特殊字符(?和!)并替换为空
  • remove all periods and replace with dashes删除所有句点并用破折号替换
  • remove all remaining spaces and replace with dashes删除所有剩余的空格并用破折号替换
  • everything lowercase全部小写

So far I have the following code written out, and I'm trying to figure out how to streamline it.到目前为止,我已经编写了以下代码,并且正在尝试弄清楚如何简化它。

                          {{ $step1_title := replace .title "_" " " }}
                          {{ $step2_title := replace $step1_title " - " "-" }}
                          {{ $step3_title := replace $step2_title "?" "" }}
                          {{ $step4_title := replace $step3_title "." "-" }}
                          {{ $step5_title := replace $step4_title " " "-" | lower }}
                          {{ $step5_title }}

It seems like a lot of steps, and doesn't account for both?似乎有很多步骤,并且不能同时考虑两者? and.. I'm assuming replaceRE would be more appropriate for that step, yet it won't recognize those characters when defined with [?]|[!] .并且..我假设replaceRE更适合该步骤,但是当使用[?]|[!]定义时,它不会识别这些字符。 Any pointers for making the above more efficient would be much appreciated.任何使上述更有效的指针将不胜感激。

For others looking for an answer - Hugo does actually have a built-in function called urlize that does exactly this!对于其他寻找答案的人 - Hugo 实际上有一个内置的 function 称为urlize正是这样做的!

https://gohugo.io/functions/urlize/ https://gohugo.io/functions/urlize/

For the curious, there is an opposite function that "humanizes" the string and returns it with separators removed and the first letter capitalized:出于好奇,有一个相反的 function 将字符串“人性化”并返回它并删除分隔符并将第一个字母大写:

https://gohugo.io/functions/humanize/ https://gohugo.io/functions/humanize/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM