【start:2023.06.23】
可以按如下方式编写markdown文件
在VS Code中,为你的.md新建一个文件夹,然后把.md文件、所有在.md文件中插入的图片放入该文件夹中
【ref】【VS Code】Visual Studio Code基本设置&快捷方式&markdown格式自动化等实用插件 | 总结
目标:
在.md文件中插入(粘贴)图片;
将图片转存为.png文件;
可以设置插入图片的尺寸
在VS Code中,安装paste image
插件,在插件的config的Insert Pattern
模块中
把:
${imageSyntaxPrefix}${imageFilePath}${imageSyntaxSuffix}
改成:
<div align=center><img width = '400' src = '${imageFilePath}'></div>
修改后结果如图:
设置默认快捷键为:Ctrl+Alt+v
这样就可以用快捷键快速地在.md文件中粘贴图片、转存图片到文件夹了
效果如下:
适用于如下情况:
如果某张图片在.md中有被使用,他会以以下格式在md文件中展示:
<div align=center><img width = '600' src = '2023-06-18-23-09-41.png'></div>
但是如果我们不需要这张图片了,仅仅删除这条html语句是不够的,因为图片文件在文件夹中仍旧存在,会占用一定的空间
为了清除冗余文件,释放空间,我们可以使用以下代码
调用函数删除.md文件中未使用的图片
# 调用函数删除md中未使用的图片
# 目标图片尺寸需要自定义,如{\d*}表示任何数字组合,{6\d*}代表6开头的数字组合
import os
import re
def delete_unused_images(md_file, image_folder):
# 读取Markdown文件内容
with open(md_file, 'r', encoding='utf-8') as file:
content = file.read()
# 查找Markdown文件中的图片链接
# 案例:
# 加括号会返回括号内的内容,不加括号则会返回完整的findall的内容
image_links = re.findall(r"", content)
print(f'image_links : {image_links}')
print(f'len image_links : {len(image_links)}')
# 遍历文件夹中的图片文件
for filename in os.listdir(image_folder):
if filename.endswith(".png"):
image_path = os.path.join(image_folder, filename)
# 如果图片链接不在Markdown文件中,删除图片文件
image_name = os.path.basename(image_path)
if image_name not in image_links:
os.remove(image_path)
print(f"Deleted unused image: {image_name}")
# 指定Markdown文件和图片文件夹的路径
md_file_path = r'C:\Users\lenovo\Desktop\矩阵论\note\main.md'
image_folder_path = r'C:\Users\lenovo\Desktop\矩阵论\note'
# 调用函数删除未使用的图片
delete_unused_images(md_file_path, image_folder_path)
'''
return:
image_links : ['2023-06-23-00-47-30.png']
Deleted unused image: 2023-06-23-01-21-30.png
Deleted unused image: 2023-06-23-01-21-33.png
'''
为md所有标题添加多级序号
# 为md所有标题添加多级序号
import re
def add_heading_numbers(markdown):
"""Adds numbering to all headings in a Markdown string.
Args:
markdown (str): A string in Markdown format.
Returns:
The Markdown string with numbers added to all headings.
"""
pattern = r'^(#+)\s+(.+)$'
headings = re.findall(pattern, markdown, flags=re.MULTILINE)
level_stack = [0] * 6
numbered_headings = []
for level, title in headings:
level = len(level)
level_stack[level] += 1
level_stack[level+1:] = [0] * (5-level)
number = '.'.join(str(x) for x in level_stack[1:level+1])
numbered_headings.append(f"{'#' * level} {number} {title}")
numbered_markdown = re.sub(pattern, lambda m: numbered_headings.pop(0), markdown, flags=re.MULTILINE)
return numbered_markdown
markdown = """
# This is a level-1 heading
## This is a level-2 heading
### This is a level-3 heading
#### This is a level-4 heading
# Another level-1 heading
## Another level-2 heading
"""
numbered_markdown = add_heading_numbers(markdown)
print(numbered_markdown)
'''
return:
# 1 This is a level-1 heading
## 1.1 This is a level-2 heading
### 1.1.1 This is a level-3 heading
#### 1.1.1.1 This is a level-4 heading
# 2 Another level-1 heading
## 2.1 Another level-2 heading
'''
目标:markdown标题快速编号
在VS Code中,安装Markdown All in One
插件
设置快捷键为:Shift+Alt+M
为md所有标题降(升)级
# 为md所有标题降(升)级
import re
def reduce_headings(markdown, level):
"""Reduces the level of all headings in a Markdown string by `level`.
Args:
markdown (str): A string in Markdown format.
level (int): The number of '#' symbols to remove from each heading.
Returns:
The Markdown string with all headings reduced by `level`.
"""
pattern = r'^(#+)\s+(.+)$'
def reduce_heading(match):
hashes, title = match.groups()
reduced_hashes = "#" * (len(hashes) - level)
return f"{reduced_hashes} {title}"
return re.sub(pattern, reduce_heading, markdown, flags=re.MULTILINE)
markdown = """
# This is a level-1 heading
## This is a level-2 heading
### This is a level-3 heading
#### This is a level-4 heading
### This is a level-3 heading
"""
reduced_markdown = reduce_headings(markdown, 2)
print(reduced_markdown)
'''
return:
# This is a level-1 heading
# This is a level-2 heading
## This is a level-3 heading
### This is a level-4 heading
'''
如果使用“短文版”方法时遇到了不可识别的字符,请使用下文的“长文版”方法
输入一个md文件,返回一个新的md文件
# 为md所有标题降(升)级
import re
import os
def reduce_headings(markdown, level):
"""Reduces the level of all headings in a Markdown string by `level`.
Args:
markdown (str): A string in Markdown format.
level (int): The number of '#' symbols to remove from each heading.
Returns:
The Markdown string with all headings reduced by `level`.
"""
pattern = r'^(#+)\s+(.+)$'
def reduce_heading(match):
hashes, title = match.groups()
reduced_hashes = "#" * (len(hashes) - level)
return f"{reduced_hashes} {title}"
return re.sub(pattern, reduce_heading, markdown, flags=re.MULTILINE)
def read_markdown_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
text = file.read()
return text
def write_markdown_file(file_path, content):
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
markdown_old_path = r'D:\JoeOffice\jupyter_notebook\computer_vision\instance_segmentation\MultiStar-main\my_utils\test.md'
markdown_new_path = r'D:\JoeOffice\jupyter_notebook\computer_vision\instance_segmentation\MultiStar-main\my_utils\test_2.md'
level = 1
markdown_content = read_markdown_file(markdown_old_path)
markdown_reduce_headings_content = reduce_headings(markdown_content, level=level)
path_last_father_folder = markdown_old_path.split('\\')[:-1]
path_last_father_folder = os.path.join(*path_last_father_folder)
os.makedirs(path_last_father_folder, exist_ok=True)
write_markdown_file(markdown_new_path, markdown_reduce_headings_content)
print(f'reduce headings by {level} level success! \nfrom: \n {markdown_old_path} \nto: \n{markdown_new_path}')