仅使用 lowdown 如何实现可点击链接?
How can i achieve clickable links with just using lowdown?
是否有任何 lowdown
参数用于将内容 table 转换为可点击的链接?我查看了手册页,但我没有弄明白。
也许没有任何选择?如果那样的话,我不想使用 pandoc
。那么,是否有运气可以在不使用大型工具的情况下实现这一目标?
我也试过 markdown
工具。它有 -f flag
个选项,但无论如何都不起作用。
echo "## [The start](#the-start)" | markdown -f toc
<a name="The-start"></a>
<h2><a href="#the-start">The start</a></h2>
所以回到主题。
in *.md files;
## [The start](#the-start)
我想像下面这样输出。
<h2 id="the-start"><a href="#the-start">The start</a></h2>
更新 1(Python 的解决方案):
echo "## [The start](#the-start)" | python -m markdown --extension=toc
这个python模块解决了我的问题。但是我想听听是否有可能(我的意思是不需要太多工作)使用 unix 工具如 sed、awk ...
更新 2(使用 bash 脚本的解决方案):
我根据@markp-fuso 的回答制作了这个脚本
可能会遗漏一些东西,但它现在可以使用。(任何建议都很好!)
#!/bin/bash
input="first-post.md"
while read -r line;do
[[ $line =~ ^[a-zA-Z0-9] ]] && echo "<p>${line##}<p>"
[[ $line =~ ^#" " ]] && ! [[ $line =~ ^##" "\[%* ]] && echo "<h1>${line### }</h1>"
[[ $line =~ ^##" " ]] && ! [[ $line =~ ^##" "\[%* ]] && echo "<h2>${line#### }</h2>"
IFS='[[]()]' read -r stuff name stuff ref stuff <<< $line
[[ $line =~ ^##" "\[%* ]] && echo "<h2 id=\"${ref//#}\"><a href=\"${ref}\">${name}</a></h2>"
done < $input
内部降价文件;
Test paragraph
0 value
# Header One
## Header Two
## [Table of contents](#table-of-contents)
## [Test]
它给出以下输出;
<p>Test paragraph<p>
<p>0 value<p>
<h1>Header One</h1>
<h2>Header Two</h2>
<h2 id="table-of-contents"><a href="#table-of-contents">Table of contents</a></h2>
<h2 id=""><a href="">Test</a></h2>
问题来自 ## [Test]
我还没弄明白。
从严格的bash
角度来看并使用一些简单的字符串处理...
s='## [The start](#the-start)'
IFS='[[]()]' read -r stuff name stuff ref stuff <<< "${s}" # break $s into 5 fields using square
# brackets and parens as delimiters
echo "<h2 id=\"${ref//#}\"><a href=\"${ref}\">${name}</a></h2>" # print the desired output string
这会生成:
<h2 id="the-start"><a href="#the-start">The start</a></h2>
注意: 这(显然)高度依赖于存储在变量 $s
.
中的字符串的格式
是否有任何 lowdown
参数用于将内容 table 转换为可点击的链接?我查看了手册页,但我没有弄明白。
也许没有任何选择?如果那样的话,我不想使用 pandoc
。那么,是否有运气可以在不使用大型工具的情况下实现这一目标?
我也试过 markdown
工具。它有 -f flag
个选项,但无论如何都不起作用。
echo "## [The start](#the-start)" | markdown -f toc
<a name="The-start"></a>
<h2><a href="#the-start">The start</a></h2>
所以回到主题。
in *.md files;
## [The start](#the-start)
我想像下面这样输出。
<h2 id="the-start"><a href="#the-start">The start</a></h2>
更新 1(Python 的解决方案):
echo "## [The start](#the-start)" | python -m markdown --extension=toc
这个python模块解决了我的问题。但是我想听听是否有可能(我的意思是不需要太多工作)使用 unix 工具如 sed、awk ...
更新 2(使用 bash 脚本的解决方案):
我根据@markp-fuso 的回答制作了这个脚本 可能会遗漏一些东西,但它现在可以使用。(任何建议都很好!)
#!/bin/bash
input="first-post.md"
while read -r line;do
[[ $line =~ ^[a-zA-Z0-9] ]] && echo "<p>${line##}<p>"
[[ $line =~ ^#" " ]] && ! [[ $line =~ ^##" "\[%* ]] && echo "<h1>${line### }</h1>"
[[ $line =~ ^##" " ]] && ! [[ $line =~ ^##" "\[%* ]] && echo "<h2>${line#### }</h2>"
IFS='[[]()]' read -r stuff name stuff ref stuff <<< $line
[[ $line =~ ^##" "\[%* ]] && echo "<h2 id=\"${ref//#}\"><a href=\"${ref}\">${name}</a></h2>"
done < $input
内部降价文件;
Test paragraph
0 value
# Header One
## Header Two
## [Table of contents](#table-of-contents)
## [Test]
它给出以下输出;
<p>Test paragraph<p>
<p>0 value<p>
<h1>Header One</h1>
<h2>Header Two</h2>
<h2 id="table-of-contents"><a href="#table-of-contents">Table of contents</a></h2>
<h2 id=""><a href="">Test</a></h2>
问题来自 ## [Test]
我还没弄明白。
从严格的bash
角度来看并使用一些简单的字符串处理...
s='## [The start](#the-start)'
IFS='[[]()]' read -r stuff name stuff ref stuff <<< "${s}" # break $s into 5 fields using square
# brackets and parens as delimiters
echo "<h2 id=\"${ref//#}\"><a href=\"${ref}\">${name}</a></h2>" # print the desired output string
这会生成:
<h2 id="the-start"><a href="#the-start">The start</a></h2>
注意: 这(显然)高度依赖于存储在变量 $s
.