Skip to content

Commit 98af732

Browse files
committed
chore(ci): blog sync
1 parent ce5b435 commit 98af732

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

data/blog/post-40.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: 发现一个掘金的bug,我给出了解决办法
3+
date: 2025-01-08T05:26:20Z
4+
slug: post-40
5+
author: chaseFunny:https://github.com/chaseFunny
6+
tags: ["bug"]
7+
---
8+
9+
大家好,我是 luckySnail ,最近在发文章的时候,发现一个 bug,当使用 markdown 链接格式`[]()` 的时候,如果链接中包含空格,就会出现下面这种情况。
10+
11+
![image-20250108131542182](https://blog-1304565468.cos.ap-shanghai.myqcloud.com/work/image-20250108131542182.png)
12+
13+
非常影响体验,但是链接中携带空格是非常常见的现象。于是打算解决一下
14+
15+
## 解决
16+
17+
项目使用 markdown渲染和编辑都是 bytemd 。于是看看掘金,发现也存在这种情况。
18+
19+
其实解决方法也很简单,就是把链接中的空格替换为:`%20`
20+
21+
```js
22+
// 替换链接中的空格为 %20
23+
const encodedValue = value.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, url) => {
24+
const encodedUrl = url.replace(/ /g, '%20');
25+
return `[${text}](${encodedUrl})`;
26+
});
27+
```
28+
29+
在 URL 中,空格是一个不安全的字符,所以我们需要进行编码。`%20` 是空格的 URL 编码形式。URL 编码(也称为百分号编码)是一种将 URL 中的特殊字符转换为安全格式的方式,这里我们使用正则表达式将 Markdown 链接中的空格替换为 `%20`,也就保证了链接在解析时不会因为空格而出现问题
30+
31+
32+
33+
---
34+
此文自动发布于:<a href="https://github.com/coderPerseus/blog/issues/40" target="_blank">github issues</a>

0 commit comments

Comments
 (0)