Skip to content

搞英语 → 看世界

翻译英文优质信息和名人推特

Menu
  • 首页
  • 作者列表
  • 独立博客
  • 专业媒体
  • 名人推特
  • 邮件列表
  • 关于本站
Menu

将行内脚注转换为 Markdown 中的常规脚注

Posted on 2023-02-24

徽标.png

在我的个人网站使用Hugo 0.25.1将近六年之后,我终于决定升级它。我稍后会解释原因。我遇到的一个问题是,我必须将 Markdown 中的行内脚注转换为常规脚注(因为 Hugo 0.25.1 支持前者,但 Hugo 从后来的某个版本开始不再支持它)。也就是说,我需要转换

text^[this is a footnote]

到

text[^1] [^1]: this is a footnote

我可以使用 Pandoc 来做到这一点,但由于我有几千篇文章,我不想让它们都经过 Pandoc 转换,以避免意外损坏。相反,我只是写了一个小的 R 脚本来帮助:

 files = list.files('.', '[.]R?md$', recursive = TRUE, full.names = TRUE) for (f in files) { x = xfun::read_utf8(f) r = '\\^\\[([^]]+)\\]' # eyes bleeding... if (length(grep(r, x)) == 0) next fn = NULL m = gregexpr(r, x) regmatches(x, m) = lapply(regmatches(x, m), function(z) { if ((n <- length(z)) == 0) return(z) z2 = sprintf('[^%d]', length(fn) + seq_len(n)) fn <<- c(fn, gsub(r, '\\1', z)) z2 }) fn = sprintf('[^%d]: %s', seq_along(fn), fn) x = c(x, rbind('', fn)) xfun::write_utf8(x, f) }

基本上,它读取一个.md (或.Rmd )文件,检测文本中^[text]形式的所有脚注,用[^n]替换它们(其中n是脚注编号),附加新脚注[^n]: text到文本,并写出到原始文件。由于我的文件处于版本控制之下,我可以手动检查 GIT 更改以确认脚本是否符合我的预期。

它确实帮助了我很多,而且我只有一个特例——包含]的脚注,例如^[this is a [footnote](url)] 。幸运的是,我没有太多带有这种脚注的帖子,所以我手动更正了它们。

为什么要升级 Hugo?我真的没有充分的理由。一个原因是由于 Hugo 的旧版本,我已经厌倦了不得不使用.Scratch 。另一个原因是作为blogdown包的维护者,我想关注 Hugo 的更新版本。对于普通用户,通常我不建议升级 Hugo,只要某个版本适合你就行。

原文: https://yihui.org/en/2023/02/markdown-footnotes/

本站文章系自动翻译,站长会周期检查,如果有不当内容,请点此留言,非常感谢。
  • Abhinav
  • Abigail Pain
  • Adam Fortuna
  • Alberto Gallego
  • Alex Wlchan
  • Answer.AI
  • Arne Bahlo
  • Ben Carlson
  • Ben Kuhn
  • Bert Hubert
  • Bits about Money
  • Brian Krebs
  • ByteByteGo
  • Chip Huyen
  • Chips and Cheese
  • Christopher Butler
  • Colin Percival
  • Cool Infographics
  • Dan Sinker
  • David Walsh
  • Dmitry Dolzhenko
  • Dustin Curtis
  • Elad Gil
  • Ellie Huxtable
  • Ethan Marcotte
  • Exponential View
  • FAIL Blog
  • Founder Weekly
  • Geoffrey Huntley
  • Geoffrey Litt
  • Greg Mankiw
  • Henrique Dias
  • Hypercritical
  • IEEE Spectrum
  • Investment Talk
  • Jaz
  • Jeff Geerling
  • Jonas Hietala
  • Josh Comeau
  • Lenny Rachitsky
  • Liz Danzico
  • Lou Plummer
  • Luke Wroblewski
  • Matt Baer
  • Matt Stoller
  • Matthias Endler
  • Mert Bulan
  • Mostly metrics
  • News Letter
  • NextDraft
  • Non_Interactive
  • Not Boring
  • One Useful Thing
  • Phil Eaton
  • Product Market Fit
  • Readwise
  • ReedyBear
  • Robert Heaton
  • Ruben Schade
  • Sage Economics
  • Sam Altman
  • Sam Rose
  • selfh.st
  • Shtetl-Optimized
  • Simon schreibt
  • Slashdot
  • Small Good Things
  • Taylor Troesh
  • Telegram Blog
  • The Macro Compass
  • The Pomp Letter
  • thesephist
  • Thinking Deep & Wide
  • Tim Kellogg
  • Understanding AI
  • 英文媒体
  • 英文推特
  • 英文独立博客
©2025 搞英语 → 看世界 | Design: Newspaperly WordPress Theme