Skip to content

Commit

Permalink
📝 优化模板index变量的i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
Achuan-2 committed Dec 2, 2024
1 parent 34c7758 commit c872c1a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
17 changes: 14 additions & 3 deletions README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

使用思源的块引实现脚注和备注功能

![](https://fastly.jsdelivr.net/gh/Achuan-2/PicBed/assets/脚注插件支持编号-2024-11-30.gif)
![](https://fastly.jsdelivr.net/gh/Achuan-2/PicBed/assets/脚注插件功能预览-2024-12-02.gif)

> 使用Tsundoku主题演示

## 📝更新日志

Expand Down Expand Up @@ -81,7 +80,19 @@ v1.1.6 / 2024.11.30 脚注支持脚注数字编号啦!
* **选中文本的样式**:选择无样式或自定义样式。默认:`无样式`​。
* **脚注自动数字编号**:使用数字编号(如[1], [2]等)替代自定义锚文本。开启后每次新建和删除脚注会自动对所有脚注重新排序编号。
注意:目前开启此项,当脚注数量越多,排序耗时越长,介意请勿开启。
* **脚注内容模板**:设置生成脚注内容的样式,推荐使用嵌套引述块或超级块来存放脚注内容,保证脚注内容属于同一个块,`${selection}`​代表选中文本的内容,`${content}`​代表脚注内容占位,`${refID}`​代表选中文本所在的块的ID,`${index}`脚注编号默认带原块链接,`${index:text}`脚注编号纯文本。另外可以使用kramdown语法设置脚注内容块的块样式。
* **脚注内容模板**:设置生成脚注内容的样式,推荐使用嵌套引述块或超级块来存放脚注内容,保证脚注内容属于同一个块,`${selection}`​代表选中文本的内容,`${content}`​代表脚注内容占位,`${refID}`​代表选中文本所在的块的ID,`${index}`脚注编号默认带原块链接,`${index:text}`脚注编号纯文本。另外可以使用kramdown语法设置脚注内容块的块样式。默认:
```markdown
{{{col
${index}
{: style="width: 2.5em; flex: 0 0 auto;"}

{{{row
> ${selection}

${content}
}}}
}}}
```
* **脚注内容块的别名**:设置脚注内容块的别名,提示这个块是脚注内容,设置为空则不设置别名。默认为空。
* **自定义样式**:自定义脚注块引、添加脚注时选中文字的样式、脚注内容块的样式。默认:
```css
Expand Down
Binary file modified preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions public/i18n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"footnoteBlockref": {
"title": "Footnote Blockref Anchor Text",
"description": "Custom anchor text",
"value": "[Footnote]"
"value": "[*]"
},
"enableOrderedFootnotes": {
"title": "Enable Footnote Numbering",
Expand All @@ -88,5 +88,6 @@
"cancelReorderFootnotes": "Cancel Reorder Footnotes",
"errors": {
"noDocId": "[Block Ref Footnote Plugin] Please fill in the specified document ID"
}
},
"indexAnchor": "[*]"
}
3 changes: 2 additions & 1 deletion public/i18n/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@
"cancelReorderFootnotes": "取消脚注编号",
"errors": {
"noDocId": "【块引脚注插件】请填写指定文档ID"
}
},
"indexAnchor": "[注]"
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ export default class PluginFootnote extends Plugin {
{: style="width: 2.5em; flex: 0 0 auto;"}
{{{row
> \${selection}
\${content}
}}}
}}}
Expand Down Expand Up @@ -868,8 +870,8 @@ export default class PluginFootnote extends Plugin {
templates = templates.replace(/\$\{selection\}/g, cleanSelection);
templates = templates.replace(/\$\{content\}/g, zeroWhite);
templates = templates.replace(/\$\{refID\}/g, currentBlockId);
templates = templates.replace(/\$\{index\}/g, `<span data-type="custom-footnote-index a" data-href="siyuan://blocks/${currentBlockId}">[注]</span>`); // 支持添加脚注编号
templates = templates.replace(/\$\{index:text\}/g, `<span data-type="custom-footnote-index">[注]</span>`); // 支持添加脚注编号
templates = templates.replace(/\$\{index\}/g, `<span data-type="custom-footnote-index a" data-href="siyuan://blocks/${currentBlockId}">${this.i18n.indexAnchor}</span>`); // 支持添加脚注编号
templates = templates.replace(/\$\{index:text\}/g, `<span data-type="custom-footnote-index>${this.i18n.indexAnchor}</span>`); // 支持添加脚注编号
templates = await renderTemplates(templates);

async function renderTemplates(templates: string): Promise<string> {
Expand Down Expand Up @@ -1270,7 +1272,6 @@ export default class PluginFootnote extends Plugin {
// );
}

// TODO: 脚注内容块改为用(await getBlockDOM(footnoteId)).dom获取dom,然后使用正则表达式将span[data-type*="custom-footnote-index"]的textContent替换[注]
private async cancelReorderFootnotes(docID: string, reorderBlocks: boolean) {
// Get current document DOM
const doc = await getDoc(docID);
Expand Down Expand Up @@ -1298,7 +1299,7 @@ export default class PluginFootnote extends Plugin {
Array.from(footnoteIds).map(async footnoteId => {
let footnoteBlock = (await getBlockDOM(footnoteId)).dom;
if (footnoteBlock) {
footnoteBlock = footnoteBlock.replace(/(<span data-type=".*?custom-footnote-index[^>]*>)\[\d+\](<\/span>)/g, '$1[注]$2');
footnoteBlock = footnoteBlock.replace(/(<span data-type=".*?custom-footnote-index[^>]*>)\[\d+\](<\/span>)/g, '$1${this.i18n.indexAnchor}$2');
}
updateBlock("dom", footnoteBlock, footnoteId);
// return setBlockAttrs(footnoteId, { "name": "" });
Expand Down

0 comments on commit c872c1a

Please sign in to comment.