Giter VIP home page Giter VIP logo

siyuan-plugin-block-converter's Introduction

块转换工具

English

更新日志

使用自定义代码进行复制、粘贴和更新操作,以块为单位进行操作,目前包含以下组件:

  • 自定义块粘贴:将剪贴板内 html 粘贴至思源笔记,支持自定义规则,针对表格做了优化。
  • 自定义块复制:复制块时将把块内容按指定方法处理后再写入剪贴板。
  • 自定义块更新:使用自定义代码处理块内容并更新。
  • 表格插入助手:将 html 类型的表格块转换为思源内置表格块。(已移除)
  • 粘贴为 Html 块:将剪贴板中内容粘贴为 Html 块。(已移除)
  • 流程图生成器:将块引用形式的流程转换为 Mermaid 流程图。(已移除)

自定义块粘贴

将剪贴板内 html 粘贴至思源笔记,与普通粘贴的不同在于,针对表格做了优化,另外,使用mixmark-io/turndown: 🛏 An HTML to Markdown converter written in JavaScript (github.com)而不是 Lute 作为 html 转 markdown 工具。

使用方法

  1. 在插件设置中设置 js 代码所在文档。
  2. 在上述文档中编写 js 代码。
  3. 点击粘贴位置处块的块标->插件->自定义粘贴

js 代码应该具有如下形式,关于 filter 和 replacement 的详细描述,参见turndown 文档

{
  filter: function (node, options) {
    const fontSize = node.style.fontSize;
    const fontSizeNum = parseInt(fontSize);
    return fontSizeNum >= 22;
  },
  replacement: function (content, node, options) {
    return "# " + content;
  },
};

自定义块复制

在思源中编写 js 代码,复制块时将把块内容按指定方法处理后再写入剪贴板。

❗ 函数内容可访问全局变量,请注意风险。

使用方法

  1. 在插件设置中设置 js 代码所在文档。
  2. 在上述文档中编写 js 代码。
    • 必须要有 return 语句
    • 必须使用代码块,并明确表示是 js 代码
    • 可以给块设置‘命名’以方便区分
    • 支持直接使用的字段:
      • id: 块 Id
      • title:块所在文档名
      • name:块命名
      • markdown:块 markdow 文本
      • content:块文本,去除了 markdown 标记
      • input:整个 block 信息,详见思源笔记用户指南/请从这里开始/搜索进阶/数据库表
      • index: 复制多个块时,块索引,内部使用result += func(input, i);对内容进行拼接(func(input, i)为本步骤中编写的 js 代码)
      • inputArray(v0.2.4 新增): 复制多个块时,包含所有块input的列表
  3. 刷新界面(控制台运行location.reload(),即不支持热更新)
    • v0.2.4 以上版本:如果是已有脚本,则支持热更新,不用再刷新界面,但块标菜单显示不会更新
  4. 点击要复制的块的块标->插件->自定义复制
  • v0.2.6 以上版本:支持设置自定义快捷键
  • 函数内Lutewindow.Lute不同,为编辑器内使用的 Lute 实例(而非 Lute 类,不需要调用 Lute.New())

内部实现

//block js 代码所在块
const func = new Function(
  "input",
  "index",
  "inputArray",
  ` 
  let { title, name, content, markdown,id } = input;
  ${block.content}
  `
);

示例

以下代码将试图返回一个((20230402121202-gofgg1n '《民诉解释》第374条'))形式的文本并写入剪贴板。

const matchGroup = content.match(/(第)(.*?)(条)/);
let realTitle = input.hpath.match(/《.*?》/);
if (!realTitle) {
  realTitle = title;
}
let result = "";
if (matchGroup) {
  result = `((${id} '${realTitle}${matchGroup[1]}${chineseToNum(
    matchGroup[2]
  )}${matchGroup[3]}'))`;
} else {
  result = `((${id} '${realTitle}${content.substring(0, 5)}'))`;
}

return result;

function chineseToNum(chnStr) {
  //该函数内容省略
}

这是图片

自定义块更新

在思源中编写 js 代码,使用该代码处理块内容并更新。

❗ 无论如何,使用脚本更新块都有一定的风险,请使用多个块测试没有问题后再使用该工具进行更新,并推荐对待更新内容进行备份。

❗ 函数内容可访问全局变量,请注意风险。

使用方法

基本同自定义块复制,注意,返回值格式如下:

{
  markdown?: string;
  attrs?: { [key: string]: string };
  isDelete?: boolean;
};
  • markdown 表示更新后的块内容
  • attr 表示更新后的属性
  • 返回isDelete为 true,则会删除该块
    • 注意,由于markdown有内容才会对块进行处理,所以,若要删除该块,需要返回markdown不为空
    • 所选块的第一个块不能被删除

⚠️ 注意事项:

  • 可以将一个块更新为多个块,但只有第一个块会继承或更新属性
  • v0.2.4 以上版本:新增 inputArray 变量(详见 自定义块复制 部分),可以利用其将多个块更新为一个块,但是如果任何块返回 markdown 内容为空,为保证数据安全,不会主动将其清空
  • v0.2.6 以上版本:支持Ctrl+Z撤销(🚀 实验性)

示例

示例 1:将段落块转换为多级列表

以下代码将把文本块按一定规则转化为列表块,并清除原有的别名和命名。

const list = markdown.split("\n");
let result = "";
let i = 0;
for (const text of list) {
  let textResult = i ? text : text.replace(/(第.{1,6}条)/, "**$1** ");
  textResult = "- " + textResult;
  if (text.startsWith("(") || text.startsWith("(")) {
    textResult = "  " + textResult;
  }
  result += "\r\n" + textResult;
  i++;
}
result = result + "\r\n---\r\n";
return { markdown: result, attrs: { name: "", alias: "" } };

这是图片

示例 2:作为模板使用

以下代码生成一个嵌入块,该嵌入块将汇总其父级块的所有反向链接。

return {
  markdown: `{{SELECT * FROM blocks WHERE id IN(SELECT block_id FROM refs WHERE def_block_id='${input.parent_id}')}}`,
};

siyuan-plugin-block-converter's People

Contributors

etchnight avatar

Watchers

 avatar

siyuan-plugin-block-converter's Issues

自定义文本内容

使用->(节点关系文本)[自定义内容]表示节点的自定义内容,最好指向时显示全部内容

函数死循环问题

目前,在自定义函数中如果出现死循环,会导致思源笔记失去相应,应该增加自定义函数运行一段时间自动退出功能

为什么不捕获Excel文件的表格的某些单元格?

Excel,或Html等内容,都是富文本形式的,如果Excel选中某些单元格复制,结果如下:

<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>

<meta name=Generator content="Microsoft Excel">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
x\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<style>
<!--.font0
	{color:#000000;
	font-size:11.0pt;
	font-family:等线;
	font-weight:400;
	font-style:normal;
	text-decoration:none;}
br
	{mso-data-placement:same-cell;}
td
	{padding-top:1px;
	padding-left:1px;
	padding-right:1px;
	mso-ignore:padding;
	color:#000000;
	font-size:11.0pt;
	font-weight:400;
	font-style:normal;
	text-decoration:none;
	font-family:等线;
	mso-number-format:General;
	border:none;
	mso-background-source:auto;
	mso-pattern:auto;
	text-align:general;
	vertical-align:bottom;
	white-space:nowrap;
	mso-rotate:0;
	mso-protection:locked visible;}
-->
</style>
</head>
<body>
<!--StartFragment-->

地图
--
商人


<!--EndFragment-->
</body>

</html>

8CimtZC4At

剪切板中的表格内容分为两种,一个是纯文本,另一个是富文本

源码中可以加入捕获富文本形式的代码,那样从excel中复制的单元格可以直接粘贴到siyuan文档里面了

subgraph

以subgraph 作为关系文本保留字,带有该关键字的block将生成subgraph 而非节点

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.