Typora书写.md文件时自动生成编号

25 篇文章 0 订阅
2 篇文章 0 订阅

前言:Typora的实现基于浏览器,各个界面都是HTML语言渲染出来的,所以,我们就可以通过添加css样式表,来添加自动编号的功能

1、新建一个名称为“base.user.css”的样式文件,将下面的代码复制进去,保存

/** 
 * 说明:
 * 正文标题区: #write
 * 侧边栏的目录大纲区: .sidebar-content 
 * 要完成自动编号功能,必须借助CSS3中的如下特性:
 * 计数器:counter(基准计数器),用于计算基准计数器的值
 * 计数器增量:counter-increment,设置每次增长的量
 * 重置计数器:counter-reset,用于将当前标题的计数器重置到指定的基准计数器
 * 子代类型选择器:nth-of-type,可以从子代中选择出同一类型元素中的指定元素
 */
.sidebar-content {
    counter-reset: h1
}
 
.outline-h1 {
    counter-reset: h2
}
 
.outline-h2 {
    counter-reset: h3
}
 
.outline-h3 {
    counter-reset: h4
}
 
.outline-h4 {
    counter-reset: h5
}
 
.outline-h5 {
    counter-reset: h6
}
 
.outline-h1>.outline-item>.outline-label:before {
    counter-increment: h1;
    content: counter(h1) " "
}
 
.outline-h2>.outline-item>.outline-label:before {
    counter-increment: h2;
    content: counter(h1) "." counter(h2) " "
}
 
.outline-h3>.outline-item>.outline-label:before {
    counter-increment: h3;
    content: counter(h1) "." counter(h2) "." counter(h3) " "
}
 
.outline-h4>.outline-item>.outline-label:before {
    counter-increment: h4;
    content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " "
}
 
.outline-h5>.outline-item>.outline-label:before {
    counter-increment: h5;
    content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) " "
}
 
.outline-h6>.outline-item>.outline-label:before {
    counter-increment: h6;
    content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) " "
}

/** initialize css counter */
#write {
counter-reset: h1
}
h1 {
counter-reset: h2
}
h2 {
counter-reset: h3
}
h3 {
counter-reset: h4
}
h4 {
counter-reset: h5
}
h5 {
counter-reset: h6
}
/** put counter result into headings */
#write h1:before {
counter-increment: h1;
content: counter(h1) " "
}#write h2:before {
counter-increment: h2;
content: counter(h1) "." counter(h2) " "
}
#write h3:before,
h3.md-focus.md-heading:before /** override the default style for focused headings */ {
counter-increment: h3;
content: counter(h1) "." counter(h2) "." counter(h3) " "
}
#write h4:before,
h4.md-focus.md-heading:before {
counter-increment: h4;
content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) " "
}
#write h5:before,
h5.md-focus.md-heading:before {
counter-increment: h5;
content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) " "
}
#write h6:before,
h6.md-focus.md-heading:before {
counter-increment: h6;
content: counter(h1) "." counter(h2) "." counter(h3) "." counter(h4) "." counter(h5) "." counter(h6) " "
}
/** override the default style for focused headings */
#write>h3.md-focus:before,
#write>h4.md-focus:before,
#write>h5.md-focus:before,
#write>h6.md-focus:before,
h3.md-focus:before,
h4.md-focus:before,
h5.md-focus:before,
h6.md-focus:before {
color: inherit;
border: inherit;
border-radius: inherit;
position: inherit;
left:initial;
float: none;
top:initial;
font-size: inherit;
padding-left: inherit;
padding-right: inherit;
vertical-align: inherit;
font-weight: inherit;
line-height: inherit;
}

2、依次打开:文件->偏好设置->外观->主题->打开主题文件夹
在这里插入图片描述
在这里插入图片描述
3、将上一步保存的css文件复制粘贴进来
在这里插入图片描述
4、关闭Typora程序及所有md相关文件,重新打开Typora,随便写入一些1-6标题的文字,测试效果

在这里插入图片描述

### 回答1: 可以使用Python的os模块来读取文件夹下的视频文件名,然后使用str.format()方法将文件名和序号链接起来,最后写入md文件中。示例代码如下: ```python import os # 文件夹路径 folder_path = "path/to/your/folder" # md文件路径 md_file_path = "path/to/your/md/file" # 读取文件夹下所有文件名 file_names = os.listdir(folder_path) # 过滤出视频文件 video_file_names = [name for name in file_names if name.endswith(".mp4")] # 打开md文件 with open(md_file_path, "w") as f: # 遍历视频文件名 for i, name in enumerate(video_file_names): # 拼接序号文件名 line = "[{}]({})".format(i + 1, os.path.join(folder_path, name)) f.write(line + "\n") ``` 点击序号后,会打开本地播放器播放对应的视频文件。 注意:这段代码仅供参考,您需要根据自己的实际情况来修改。 ### 回答2: 你可以使用Python的os模块来读取文件夹下的视频文件名,然后使用Markdown语法来创建md文件,并在md文件中链接本地文件。下面是一个示例代码: ```python import os # 遍历文件夹下的视频文件 folder_path = './videos' video_files = [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))] # 创建md文件 md_file_path = 'videos.md' md_file = open(md_file_path, 'w') # 写入md文件内容 for index, video_file in enumerate(video_files): # 构建本地文件链接 file_link = os.path.join(folder_path, video_file) # 构建序号链接和视频文件名 video_name = os.path.splitext(video_file)[0] video_link = f'[{index+1}. {video_name}]({file_link})' # 写入md文件 md_file.write(video_link + '\n') # 关闭md文件 md_file.close() ``` 此代码假设视频文件夹路径为'./videos',将遍历该文件夹中的所有视频文件,并按照序号链接和文件名的格式将其写入到videos.md文件中。你可以根据实际情况修改文件夹路径和md文件路径。 在生成的md文件中,每个视频文件名都会用序号进行编号,并且作为链接展示。你可以点击链接打开本地播放器来播放对应的视频文件。 ### 回答3: 可以使用Python的os和re模块来读取文件夹下的视频文件名,并通过序号链接本地文件,存放在md文件中,点击序号可以打开本地播放器播放。 首先,需要引入os和re模块: ```python import os import re ``` 然后,可以使用os模块中的os.listdir()函数来获取文件夹下的所有文件名,并使用re模块中的re.findall()函数来筛选出视频文件名。假设视频文件都是以".mp4"或".avi"结尾的: ```python folder_path = "文件夹路径" # 替换为实际文件夹路径 video_files = [f for f in os.listdir(folder_path) if re.findall(r"\.(mp4|avi)$", f, re.IGNORECASE)] ``` 接着,可以根据视频文件名生成序号并构建md文件的内容。使用字符串的format()方法可以方便地插入序号和视频文件名: ```python md_content = "" for i, file_name in enumerate(video_files): # 生成序号 index = i + 1 # 构建md文件内容 md_content += "{}. [{}]({})\n".format(index, file_name, os.path.join(folder_path, file_name)) ``` 最后,将md文件内容保存到.md文件中: ```python md_file_path = "md文件路径" # 替换为实际md文件路径 with open(md_file_path, "w") as f: f.write(md_content) ``` 这样,就可以根据视频文件名生成md文件,并通过序号链接本地文件,存放在md文件中。点击序号即可打开本地播放器播放视频文件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值