Skip to content

Commit

Permalink
Create markdownUrls.html
Browse files Browse the repository at this point in the history
  • Loading branch information
chenglu authored Mar 10, 2024
1 parent beb0183 commit 4af5501
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions markdownUrls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Markdown Link Extractor with WeChat Link Exclusion</title>
<style>
#inputMarkdown, #outputMarkdown {
width: 90%;
height: 150px;
margin: 10px;
}
</style>
</head>
<body>

<h2>Markdown Link Extractor with WeChat Link Exclusion</h2>
<textarea id="inputMarkdown" placeholder="输入Markdown内容..."></textarea><br>
<button onclick="extractAndAppendLinks()">提取并追加链接</button><br>
<textarea id="outputMarkdown" readonly placeholder="处理后的结果..."></textarea>

<script>
function extractAndAppendLinks() {
let inputText = document.getElementById('inputMarkdown').value;
let output = document.getElementById('outputMarkdown');

// 分段处理,以换行符分割文本
let paragraphs = inputText.split('\n\n');
let processedParagraphs = paragraphs.map(paragraph => {
// 正则表达式匹配Markdown链接格式
const linkRegex = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)(?: "[^"]*")?\)/g;
let links = [];
let result;
let processedText = paragraph;

// 循环匹配并收集所有链接,跳过微信链接
while ((result = linkRegex.exec(paragraph)) !== null) {
if (!result[2].includes('mp.weixin.qq.com')) {
links.push({
title: result[1],
url: result[2]
});
}
}

// 构建并追加链接到当前段落
if (links.length > 0) {
let linksString = links.map(link => `- ${link.title}\n<url>${link.url}</url>`).join('\n');
processedText += '\n\n' + linksString; // 追加链接到当前段落下面
}

return processedText;
});

// 将处理后的段落重新拼接成一个字符串,并显示在输出框中
output.value = processedParagraphs.join('\n\n');
}
</script>

</body>
</html>

0 comments on commit 4af5501

Please sign in to comment.