markdown使用技巧
目录
查看详情
- toc.py
import sys
import os
import time
headline = ['#','##','###','####','#####','######']
lines_in_file = []
"""生成目录列表中的某一项"""
def creat_directory_line(line,headline_mark,i):
if headline_mark == '#':
return '<a href="#' + str(i) + '">' + line[2:-1] + "</a> \n"
elif headline_mark == '##':
# 为Markdown中的一种缩进,这里不直接用空格作为缩进是因为多个空格一起出现可能会生成代码块,引发歧义
return ' <a href="#' + str(i) + '">' + line[3:-1] + "</a> \n"
elif headline_mark == '###':
return '  <a href="#' + str(i) + '">' + line[4:-1] + "</a> \n"
elif headline_mark == '####':
return '   <a href="#' + str(i) + '">' + line[5:-1] + "</a> \n"
elif headline_mark == '#####':
return '    <a href="#' + str(i) + '">' + line[6:-1] + "</a> \n"
elif headline_mark == '######':
return '     <a href="#' + str(i) + '">' + line[7:-1] + "</a> \n"
"""生成目录列表"""
def creat_directory(f):
i = 0
directory = []
directory.append('<a name="index">**Index**</a>\n')
for line in f:
lines_in_file.append(line)
f.close()
length = len(lines_in_file)
for j in range(length):
splitedline = lines_in_file[j].lstrip().split(' ')
if splitedline[0] in headline:
#如果为最后一行且末尾无换行(防最后一个字被去除)
if j == length - 1 and lines_in_file[j][-1] != '\n':
directory.append(creat_directory_line(lines_in_file[j] + '\n',splitedline[0],i) + '\n')
lines_in_file[j] = lines_in_file[j].replace(splitedline[0] + ' ',splitedline[0] + ' ' + '<a name="' + str(i) + '">')[:] + '</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>' + "\n"
i = i + 1
else:
directory.append(creat_directory_line(lines_in_file[j],splitedline[0],i))
lines_in_file[j] = lines_in_file[j].replace(splitedline[0] + ' ',splitedline[0] + ' ' + '<a name="' + str(i) + '">')[:-1] + '</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>' + "\n"
i = i + 1
return directory
"""以目录列表为参数生成添加目录的文件"""
def creat_file_with_toc(f):
directory = creat_directory(f)
file_with_toc = os.getcwd() + '\\file_with_toc.md'
if not os.path.exists(file_with_toc):
with open(file_with_toc, 'w+',encoding='utf-8') as f:
for directory_line in directory:
f.write(directory_line)
for line in lines_in_file:
f.write(line)
print('文件已生成')
else:
print('文件名重复,请修改文件'+'file_with_toc.md'+'的文件名后重试')
if __name__=='__main__':
file_name = ''
#如果未传入文件名
if len(sys.argv) < 2:
path = os.getcwd()
file_and_dir = os.listdir(path)
md_file = []
for item in file_and_dir:
if item.split('.')[-1].lower() in ['md','mdown','markdown'] and os.path.isfile(item):
md_file.append(item)
if len(md_file) != 0:
print('当前目录下的Markdown文件:')
for file in md_file:
print(file)
file_name = input('请输入文件名(含后缀)\n')
else:
print('该目录下无Markdown文件,即将退出...')
time.sleep(2)
os._exit(0)
else:
file_name = sys.argv[1]
if os.path.exists(file_name) and os.path.isfile(file_name):
with open(file_name,'r',encoding='utf-8') as f:
creat_file_with_toc(f)
else:
msg = "未找到文件"
print(msg)
- README.md
###### 标题1
###### 标题2
###### 标题3
###### 标题4
###### 标题5
###### 标题6
- 打开powershell
PS C:\Users\ychen> cd C:\work\todo
PS C:\work\todo> ls
目录: C:\work\todo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023/12/29 9:05 94 README.md
-a---- 2023/12/29 9:06 3921 toc.py
PS C:\work\todo> python toc.py README.md
文件已生成
PS C:\work\todo> ls
目录: C:\work\todo
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2023/12/29 9:06 981 file_with_toc.md
-a---- 2023/12/29 9:05 94 README.md
-a---- 2023/12/29 9:06 3921 toc.py
- file_with_toc.md
<a name="index">**Index**</a>
     <a href="#0">标题1</a>
     <a href="#1">标题2</a>
     <a href="#2">标题3</a>
     <a href="#3">标题4</a>
     <a href="#4">标题5</a>
     <a href="#5">标题6</a>
###### <a name="0">标题1</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="1">标题2</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="2">标题3</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="3">标题4</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="4">标题5</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="5">标题6</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
<a name="index">**Index**</a>
* <a href="#0">标题1</a>
* <a href="#1">标题2</a>
* <a href="#2">标题3</a>
* <a href="#3">标题4</a>
* <a href="#4">标题5</a>
* <a href="#5">标题6</a>
###### <a name="0">标题1</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="1">标题2</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="2">标题3</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="3">标题4</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="4">标题5</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
###### <a name="5">标题6</a><a style="float:right;text-decoration:none;" href="#index">[Top]</a>
标题1[Top]
标题2[Top]
标题3[Top]
标题4[Top]
标题5[Top]
标题6[Top]
目录2
<style>
a.a1{
text-decoration: none;
color: black;
}
a.a2{
text-decoration: none;
color: black;
}
a.a2:hover{
color: red;
}
</style>
<details> <summary>Index</summary>
* <a href="#0" class="a2">标题1</a>
* <a href="#1" class="a2">标题2</a>
* <a href="#2" class="a2">标题3</a>
* <a href="#3" class="a2">标题4</a>
* <a href="#4" class="a2">标题5</a>
* <a href="#5" class="a2">标题6</a>
</details>
###### <a name="0" class="a1">标题1</a>
###### <a name="1" class="a1">标题2</a>
###### <a name="2" class="a1">标题3</a>
###### <a name="3" class="a1">标题4</a>
###### <a name="4" class="a1">标题5</a>
###### <a name="5" class="a1">标题6</a>
标题1
标题2
标题3
标题4
标题5
标题6
代码块
<html>
<head>
</head>
</html>
# 缩进4个空格,效果如下
<html>
<head>
</head>
</html>
表格
表格页面居中
,参考
<style>
.center
{
width: auto;
display: table;
margin-left: auto;
margin-right: auto;
}
</style>
<p align="center"><font face="黑体" size=2.>表1 示例表格</font></p>
<div class="center">
| 序号 | 内容 | 描述 |
| :---: | :---: | :---------------: |
| 1 | l | 大写字母L的小写字母l |
| 2 | I | 大写字母I |
| 3 | 1 | 数字1 |
</div>
表1 示例表格
序号 | 内容 | 描述 |
---|---|---|
1 | l | 大写字母L的小写字母l |
2 | I | 大写字母I |
3 | 1 | 数字1 |
按键
<style>
kbd {
margin: 0px 0.2em;
padding: 0.1em 0.6em;
border-radius: 3px;
border: 1px solid #ccc;
color: rgb(51, 51, 51);
font-family: "Arial Black", Impact;
display: inline-block;
box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2), 0 0 0 2px #fff inset;
background-color: rgb(247, 247, 247);
-moz-box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2), 0 0 0 2px #fff inset;
-webkit-box-shadow: 0 1px 0px rgba(0, 0, 0, 0.2), 0 0 0 2px #fff inset;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
}
</style>
使用<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Delete</kbd>重启电脑
使用Ctrl+Alt+Delete重启电脑
折叠
<details> <summary>查看详情</summary>
</details>
查看详情
折叠内容
上标、下标
a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup>
a2 + b2 = c2
拓展
Vega-lite
,参考
查看详情
- vscode安装插件
Markdown Preview Enhanced
,编写如下
```vega-lite
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"description": "A simple bar chart with embedded data.",
"width": 500,
"data": {
"values": [
{"a": "A", "b": 28}, {"a": "B", "b": 55}, {"a": "C", "b": 43},
{"a": "D", "b": 91}, {"a": "E", "b": 81}, {"a": "F", "b": 53},
{"a": "G", "b": 19}, {"a": "H", "b": 87}, {"a": "I", "b": 52}
]
},
"mark": "bar",
"encoding": {
"x": {"field": "a", "type": "nominal", "axis": {"labelAngle": 0}},
"y": {"field": "b", "type": "quantitative"}
}
}
```
- 效果