Compare commits

..

2 Commits

Author SHA1 Message Date
小能饼干 46a35668d4
Update README.md 2025-02-08 16:29:18 +08:00
laishunchao f7eb78de31 feat: 重构为2.0 2025-02-08 16:17:20 +08:00
8 changed files with 271 additions and 592 deletions

View File

@ -1,3 +1,5 @@
> **2.0 版本来了使用更简洁的js脚本流程**
# 可以解析`markdown`语法的`anki`卡片模板。
# 特点:纯文本代码模板 无需任何插件。复制就能用
@ -44,28 +46,10 @@
或者把梯子关了(或者自己设置规则) 因为默认用的是字节的cdn库
### 卡片模板创建新手指引(或直接导入现成的 `markdown.apkg` 牌组)
### 卡片模板创建新手指引(或直接导入仓库现成的 `.apkg` 牌组)
1. 正面模板内容
1. 正面模板内容 完整复制仓库 `./卡片正面内容模板.txt` 的内容
```
<div class="md-content">
{{Front}}
</div>
2. 背面内容模板 完整复制仓库 `./卡片北面面内容模板.txt` 的内容
<script>
// 这里复制demo.html中的script内容
</script>
```
2. 背面内容模板
```
{{FrontSide}}
<div class="md-content">{{Back}}</div>
```
3. 样式
复制 `./卡片样式.css` 文件的内容
3. 样式 完整复制仓库`./卡片样式.css` 文件的内容

180
demo-dev-test.html Normal file
View File

@ -0,0 +1,180 @@
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.8.1/github-markdown.min.css"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/base16/onedark.min.css"
/>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"
/>
<script
defer
src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/highlight.min.js"
></script>
<title>anki-md模板测试</title>
</head>
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
/* 初始状态下设置透明度为0 */
.md-content {
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
</style>
<body>
<!-- prettier-ignore -->
<div class="md-content">
### 我看showdown真是老糊涂了
</div>
<!-- prettier-ignore -->
<div class="md-content">
> 呵呵呵
> 呵呵呵哈哈哈哈
> 喜喜
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题
---
**加粗**
*斜体*
***加粗斜体***
---
> 引用
---
- 无序列表
- 无序列表
- 无序列表
---
1. 有序列表
2. 有序列表
3. 有序列表
---
[链接](https://www.baidu.com)
---
![图片](https://www.baidu.com/img/bd_logo1.png)
---
`行内代码`
---
```javascript
// 代码块
console.log('hello world')
```
</div>
<!--
整个script内容(包括script标签)
需要复制到anki的卡片模板中
-->
<!-- https://github.com/markdown-it/markdown-it -->
<script
defer
src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/markdown-it/12.3.2/markdown-it.min.js"
></script>
<!-- 代码高亮库 -->
<script
defer
src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/highlight.min.js"
></script>
<script defer>
"use strict";
// 在anki中通过窗口查看调试信息 没控制台啊
function consoleLog(str) {
var div = document.createElement("div");
div.innerHTML = str;
document.body.appendChild(div);
}
// 清除br标签
// anki自动生成的<br>全部干掉 严格按照原生换行来写
var clearBR = (str) => {
str = str.replace(/<br>/g, "\r\n");
return str;
};
// 清除空格实体字符
var clearBlankNbsp = (str) => {
str = str.replace(/&nbsp;/g, " ");
return str;
};
// 反转义 HTML 实体,确保卡片中所有的符号正常
// Eg. #include <stdio.h>
var unescapeHTMLEntities = (innerHTML) =>
Object.assign(document.createElement("textarea"), { innerHTML }).value;
// 解析入口main方法
var parseMarkDownFn = () => {
const md = markdownit({
html: true, // 若为 false 将导致 anki 自带的公式渲染失效。
linkify: true,
typographer: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value;
} catch (__) {}
}
return ""; // use external default escaping
},
});
document.querySelectorAll(".md-content").forEach((div, index) => {
console.log("查找到的容器元素", div);
// 需要去除首尾空格 不然markdown-it会解析错误
var text = unescapeHTMLEntities(div.innerHTML).trim();
console.log("text", text);
// 清除br标签
text = clearBR(text);
// 👇关键核心md转换
var html = md.render(text);
var newDiv = document.createElement("div");
var hr = document.createElement("hr");
newDiv.innerHTML = html;
// 激活github-markdown-css
newDiv.className = "markdown-body";
div.parentNode.insertBefore(newDiv, div.nextSibling);
index === 1 ? div.parentNode.insertBefore(hr, div.nextSibling) : null;
div.className = `x-${index}`;
div.style.display = "none";
// 不能从body直接插入元素 anki 不刷新的
});
};
// TEMP 复制到卡片中要打开注释 去掉下边的代码。
// parseMarkDownFn();
// TEMP 在浏览器中测试的时候使用
document.addEventListener("DOMContentLoaded", function () {
// 你的主要代码
"use strict";
parseMarkDownFn();
});
</script>
</body>
</html>

205
demo.html
View File

@ -1,205 +0,0 @@
<!DOCTYPE html>
<html lang="cn">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>anki-md模板测试</title>
</head>
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
/* 初始状态下设置透明度为0 */
.md-content {
opacity: 0;
transition: opacity 0.3s ease-in-out;
}
</style>
<body>
<!-- prettier-ignore -->
<div class="md-content">
### 我看showdown真是老糊涂了
</div>
<!-- prettier-ignore -->
<div class="md-content">
> 呵呵呵
> 呵呵呵哈哈哈哈
> 喜喜
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题
---
**加粗**
*斜体*
***加粗斜体***
---
> 引用
---
- 无序列表
- 无序列表
- 无序列表
---
1. 有序列表
2. 有序列表
3. 有序列表
---
[链接](https://www.baidu.com)
---
![图片](https://www.baidu.com/img/bd_logo1.png)
---
`行内代码`
---
```javascript
// 代码块
console.log('hello world')
```
</div>
<!--
整个script内容(包括script标签)
需要复制到anki的卡片模板中
-->
<script>
'use strict'
// 在anki中查看调试信息 没控制台啊
function consoleLog(str) {
var div = document.createElement('div')
div.innerHTML = str
document.body.appendChild(div)
}
// https://github.com/markdown-it/markdown-it
var MARKDOWN_IT_CDN =
'https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/markdown-it/12.3.2/markdown-it.min.js'
// 代码高亮库
var HIGHLIGHT_JS_CDN =
'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/highlight.min.js'
// 代码高亮样式
var HIGHLIGHT_CSS_CDN =
'https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/styles/base16/onedark.min.css'
// github-markdown-css
var GITHUB_MARKDOWN_CSS_CDN =
'https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/github-markdown-css/5.1.0/github-markdown.min.css'
// 加载库文件
var createScript = (src) => {
var scriptElement = document.createElement('script')
scriptElement.src = src
scriptElement.async = true
scriptElement.type = 'text/javascript'
document.head.appendChild(scriptElement)
return new Promise((resolve) => {
scriptElement.onload = function () {
resolve(src)
}
})
}
// 加载css文件
var createLink = (url) => {
var linkElement = document.createElement('link')
linkElement.rel = 'stylesheet'
linkElement.href = url
linkElement.onload = () => {}
document.head.appendChild(linkElement)
}
createLink(GITHUB_MARKDOWN_CSS_CDN)
createLink(HIGHLIGHT_CSS_CDN)
// 清除br标签
// anki自动生成的<br>全部干掉 严格按照原生换行来写
var clearBR = (str) => {
str = str.replace(/<br>/g, '\r\n')
return str
}
// 清除空格实体字符
var clearBlankNbsp = (str) => {
str = str.replace(/&nbsp;/g, ' ')
return str
}
// 反转义 HTML 实体,确保卡片中所有的符号正常
// Eg. #include <stdio.h>
var unescapeHTMLEntities = (innerHTML) =>
Object.assign(document.createElement('textarea'), { innerHTML }).value
// 解析入口main方法
var parseMarkDownFn = () => {
const md = markdownit({
html: true, // 若为 false 将导致 anki 自带的公式渲染失效。
linkify: true,
typographer: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value
} catch (__) {}
}
return '' // use external default escaping
},
})
document.querySelectorAll('.md-content').forEach((div, index) => {
console.log('查找到的容器元素', div)
// 需要去除首尾空格 不然markdown-it会解析错误
var text = unescapeHTMLEntities(div.innerHTML).trim()
console.log('text', text)
// 清除br标签
text = clearBR(text)
// 👇关键核心md转换
var html = md.render(text)
var newDiv = document.createElement('div')
var hr = document.createElement('hr')
newDiv.innerHTML = html
// 激活github-markdown-css
newDiv.className = 'markdown-body'
div.parentNode.insertBefore(newDiv, div.nextSibling)
index === 1 ? div.parentNode.insertBefore(hr, div.nextSibling) : null
div.className = `x-${index}`
div.style.display = 'none'
// 不能从body直接插入元素 anki 不刷新的
})
}
createScript(HIGHLIGHT_JS_CDN)
.then(() => {
return createScript(MARKDOWN_IT_CDN)
})
.then(() => {
// anki模板无法使用window.onload我怀疑是被占用了
parseMarkDownFn()
})
.then(() => {
// 清除重复项 试试
var repeatEl = document.querySelectorAll('.x-0')
repeatEl.forEach((el, i) => {
if (i === repeatEl.length - 1) {
return
}
document.body.removeChild(el)
})
})
</script>
</body>
</html>

Binary file not shown.

View File

@ -1,5 +1,8 @@
@import url('https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.8.1/github-markdown.min.css');
@import url('https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.11.1/styles/base16/onedark.min.css');
@import url('https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css');
/*
把这里的样式代码 复制到卡片模板 的样式区域
把这里的样式代码 ctrl a 复制到卡片模板 的样式区域
*/
/* 初始状态下设置透明度为0 */
.md-content {
@ -7,371 +10,10 @@
transition: opacity 0.3s ease-in-out;
}
/* github样式 */
/* 卡片内容展示样式 */
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 32px;
}
/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
/* Document
========================================================================== */
/**
* 1. Correct the line height in all browsers.
* 2. Prevent adjustments of font size after orientation changes in iOS.
*/
html {
line-height: 1.15; /* 1 */
-webkit-text-size-adjust: 100%; /* 2 */
width: 100%;
height: 100%;
}
/* Sections
========================================================================== */
/**
* Remove the margin in all browsers.
*/
body {
margin: 0;
background: #fff;
width: 100%;
height: 100%;
text-align: left;
}
/**
* Render the `main` element consistently in IE.
*/
main {
display: block;
}
/**
* Correct the font size and margin on `h1` elements within `section` and
* `article` contexts in Chrome, Firefox, and Safari.
*/
h1 {
font-size: 2em;
margin: 0.67em 0;
}
/* Grouping content
========================================================================== */
/**
* 1. Add the correct box sizing in Firefox.
* 2. Show the overflow in Edge and IE.
*/
hr {
box-sizing: content-box; /* 1 */
height: 0; /* 1 */
overflow: visible; /* 2 */
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
pre {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/* Text-level semantics
========================================================================== */
/**
* Remove the gray background on active links in IE 10.
*/
a {
background-color: transparent;
}
/**
* 1. Remove the bottom border in Chrome 57-
* 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari.
*/
abbr[title] {
border-bottom: none; /* 1 */
text-decoration: underline; /* 2 */
text-decoration: underline dotted; /* 2 */
}
/**
* Add the correct font weight in Chrome, Edge, and Safari.
*/
b,
strong {
font-weight: bolder;
}
/**
* 1. Correct the inheritance and scaling of font size in all browsers.
* 2. Correct the odd `em` font sizing in all browsers.
*/
code,
kbd,
samp {
font-family: monospace, monospace; /* 1 */
font-size: 1em; /* 2 */
}
/**
* Add the correct font size in all browsers.
*/
small {
font-size: 80%;
}
/**
* Prevent `sub` and `sup` elements from affecting the line height in
* all browsers.
*/
sub,
sup {
font-size: 75%;
line-height: 0;
position: relative;
vertical-align: baseline;
}
sub {
bottom: -0.25em;
}
sup {
top: -0.5em;
}
/* Embedded content
========================================================================== */
/**
* Remove the border on images inside links in IE 10.
*/
img {
border-style: none;
}
/* Forms
========================================================================== */
/**
* 1. Change the font styles in all browsers.
* 2. Remove the margin in Firefox and Safari.
*/
button,
input,
optgroup,
select,
textarea {
font-family: inherit; /* 1 */
font-size: 100%; /* 1 */
line-height: 1.15; /* 1 */
margin: 0; /* 2 */
}
/**
* Show the overflow in IE.
* 1. Show the overflow in Edge.
*/
button,
input {
/* 1 */
overflow: visible;
}
/**
* Remove the inheritance of text transform in Edge, Firefox, and IE.
* 1. Remove the inheritance of text transform in Firefox.
*/
button,
select {
/* 1 */
text-transform: none;
}
/**
* Correct the inability to style clickable types in iOS and Safari.
*/
button,
[type='button'],
[type='reset'],
[type='submit'] {
-webkit-appearance: button;
}
/**
* Remove the inner border and padding in Firefox.
*/
button::-moz-focus-inner,
[type='button']::-moz-focus-inner,
[type='reset']::-moz-focus-inner,
[type='submit']::-moz-focus-inner {
border-style: none;
padding: 0;
}
/**
* Restore the focus styles unset by the previous rule.
*/
button:-moz-focusring,
[type='button']:-moz-focusring,
[type='reset']:-moz-focusring,
[type='submit']:-moz-focusring {
outline: 1px dotted ButtonText;
}
/**
* Correct the padding in Firefox.
*/
fieldset {
padding: 0.35em 0.75em 0.625em;
}
/**
* 1. Correct the text wrapping in Edge and IE.
* 2. Correct the color inheritance from `fieldset` elements in IE.
* 3. Remove the padding so developers are not caught out when they zero out
* `fieldset` elements in all browsers.
*/
legend {
box-sizing: border-box; /* 1 */
color: inherit; /* 2 */
display: table; /* 1 */
max-width: 100%; /* 1 */
padding: 0; /* 3 */
white-space: normal; /* 1 */
}
/**
* Add the correct vertical alignment in Chrome, Firefox, and Opera.
*/
progress {
vertical-align: baseline;
}
/**
* Remove the default vertical scrollbar in IE 10+.
*/
textarea {
overflow: auto;
}
/**
* 1. Add the correct box sizing in IE 10.
* 2. Remove the padding in IE 10.
*/
[type='checkbox'],
[type='radio'] {
box-sizing: border-box; /* 1 */
padding: 0; /* 2 */
}
/**
* Correct the cursor style of increment and decrement buttons in Chrome.
*/
[type='number']::-webkit-inner-spin-button,
[type='number']::-webkit-outer-spin-button {
height: auto;
}
/**
* 1. Correct the odd appearance in Chrome and Safari.
* 2. Correct the outline style in Safari.
*/
[type='search'] {
-webkit-appearance: textfield; /* 1 */
outline-offset: -2px; /* 2 */
}
/**
* Remove the inner padding in Chrome and Safari on macOS.
*/
[type='search']::-webkit-search-decoration {
-webkit-appearance: none;
}
/**
* 1. Correct the inability to style clickable types in iOS and Safari.
* 2. Change font properties to `inherit` in Safari.
*/
::-webkit-file-upload-button {
-webkit-appearance: button; /* 1 */
font: inherit; /* 2 */
}
/* Interactive
========================================================================== */
/*
* Add the correct display in Edge, IE 10+, and Firefox.
*/
details {
display: block;
}
/*
* Add the correct display in all browsers.
*/
summary {
display: list-item;
}
/* Misc
========================================================================== */
/**
* Add the correct display in IE 10+.
*/
template {
display: none;
}
/**
* Add the correct display in IE 10.
*/
[hidden] {
display: none;
}
padding: 16px;
}

View File

@ -0,0 +1,75 @@
<div class="md-content">
{{Front}}
</div>
<!-- https://github.com/markdown-it/markdown-it -->
<script defer src="https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/markdown-it/12.3.2/markdown-it.min.js"></script>
<!-- 代码高亮库 -->
<script defer src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/highlight.js/11.4.0/highlight.min.js"></script>
<script defer>
'use strict'
// 在anki中通过窗口查看调试信息 没控制台啊
function consoleLog(str) {
var div = document.createElement('div')
div.innerHTML = str
document.body.appendChild(div)
}
// 清除br标签
// anki自动生成的<br>全部干掉 严格按照原生换行来写
var clearBR = (str) => {
str = str.replace(/<br>/g, '\r\n')
return str
}
// 清除空格实体字符
var clearBlankNbsp = (str) => {
str = str.replace(/&nbsp;/g, ' ')
return str
}
// 反转义 HTML 实体,确保卡片中所有的符号正常
// Eg. #include <stdio.h>
var unescapeHTMLEntities = (innerHTML) =>
Object.assign(document.createElement('textarea'), { innerHTML }).value
// 解析入口main方法
var parseMarkDownFn = () => {
const md = markdownit({
html: true, // 若为 false 将导致 anki 自带的公式渲染失效。
linkify: true,
typographer: true,
breaks: true,
highlight: function (str, lang) {
if (lang && hljs.getLanguage(lang)) {
try {
return hljs.highlight(str, { language: lang }).value
} catch (__) {}
}
return '' // use external default escaping
},
})
document.querySelectorAll('.md-content').forEach((div, index) => {
console.log('查找到的容器元素', div)
// 需要去除首尾空格 不然markdown-it会解析错误
var text = unescapeHTMLEntities(div.innerHTML).trim()
console.log('text', text)
// 清除br标签
text = clearBR(text)
// 👇关键核心md转换
var html = md.render(text)
var newDiv = document.createElement('div')
var hr = document.createElement('hr')
newDiv.innerHTML = html
// 激活github-markdown-css
newDiv.className = 'markdown-body'
div.parentNode.insertBefore(newDiv, div.nextSibling)
index === 1 ? div.parentNode.insertBefore(hr, div.nextSibling) : null
div.className = `x-${index}`
div.style.display = 'none'
// 不能从body直接插入元素 anki 不刷新的
})
}
parseMarkDownFn();
</script>

View File

@ -0,0 +1,3 @@
{{FrontSide}}
<div class="md-content">{{Back}}</div>