[英]Markdown, changing the referencing from exported documents to simpler links, stuck in JS
所以我想在從文字處理器導出的降價文檔中進行搜索和替換。 基本上擺脫了文本鏈接中支持或更簡單的引用,以便於更新/更改/添加。 同時與 kramdown 兼容。
我被困在這個正確匹配但不起作用的 JS 上。
這是降價:
// content is defined somewhere, let's put it in a "content" variable
const content = `What is Lorem Ipsum?
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and this<sup>[[Pubmed]](1)</sup> book. This<sup>[[Microsoft](3)</sup> not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using something<sup>[[Wikipedia]](2)</sup>.
[1]: https://pubmed.com
[2]: https://wikipedia.org
[3]: https://microsoft.com
`
然后運行
// define our function to extract it
const extractCitationsFromMarkdown = content => {
// Array of regexes - first pulls the in-content links (between sup tags) but only for in those 3 types Pubmed|Microsoft|Wikipedia
// second one for matching the references at the bottom
const regexes = [
/\<sup\>\[\[(Pubmed|Microsoft|Wikipedia)\]\]\((\d+)\)<\/sup\>/mg,
/\[(\d+)\]: ([^\s]+)/mg
]
// Extract the matches from the text
const matches = regexes
.map(re => Array.from(content.matchAll(re)))
.map(groups => groups.map(g => g.slice(1)))
// format the results
return matches.at(0)
.map(([reference, referenceNumber ]) =>
([
`[${reference}]`,
matches.at(1).find(group => group.includes(referenceNumber)).at(1),
]).join(': ')
).toString(/\n/)
}
調用它:
extractCitationsFromMarkdown(content)
預期的MD結果:
What is Lorem Ipsum? Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and this<sup>[[Pubmed]](https://pubmed.com)</sup> book. This<sup>[[Microsoft](https://microsoft.com)</sup> not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using something<sup>[[Wikipedia]](https://wikipedia.org)</sup>.
預期的渲染結果:
什么是 Lorem Ipsum? 自 1500 年代以來,Lorem Ipsum 一直是業界標准的虛擬文本,當時一位不知名的印刷商采用了一種類型的廚房和這本[Pubmed]書。 這[microsoft.com]不僅五個世紀,而且還向電子排版的飛躍,基本保持不變。
使用 Lorem Ipsum 的重點是它具有或多或少的正態分布字母,而不是使用某些東西[Wikipedia] 。
任何幫助將不勝感激,在此停留 > 1 天。
謝謝
您的函數返回一個匹配的字符串,但不執行任何替換。
請注意,輸入中缺少右方括號; 這里:
<sup>[[Microsoft](https://microsoft.com)</sup>
^
我將提出一個執行搜索和替換的函數,遵循以下過程:
function makeCitationsInline(content) { const regex = /(\<sup\>\[\[.*?\]\]\()(\d+)(\)<\/sup\>)/g; // Collect the references that are used in the text const legend = Object.fromEntries( Array.from(content.matchAll(regex), m => [m[2], m[2]]) ); // Extract those references from the footer return content.replace(/\[(\d+)\]: ([^\s]+)\s*/g, (m, i, url) => (legend[i] &&= url) ? "" : m // Insert them inline ).replace(regex, (_, pre, i, post) => pre + legend[i] + post); } const content = `What is Lorem Ipsum? Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and this<sup>[[Pubmed]](1)</sup> book. This<sup>[[Microsoft]](3)</sup> not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using something<sup>[[Wikipedia]](2)</sup>. [1]: https://pubmed.com [2]: https://wikipedia.com [3]: https://microsoft.com `; console.log(makeCitationsInline(content));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.