链接 ​

选中文字后为它附加地址。如果没有选择文字就输入地址,地址本身会作为链接文字插入。输入 http:// 或 https:// 地址后按 Space 或 Enter,也会自动变成链接。

链接只保存 http:、https:,以及以 . 或 / 开头的同站路径。像 javascript: 或 //example.com 这样无法明确识别 origin 的地址会被拒绝。上传生成的附件链接还会保存文件信息,不能像普通链接一样手动创建。

翅膀
默认字体高度
HTML正在加载编辑器…
JSONnabi-tree正在加载编辑器…
安装
npm install nabi-note
代码
import {
  createLocale, createNabiWith, mountSurface, mountToolbar,
  mountContextToolbar, mountHints, watchSettle, subscriptWing, linkWing,
  footnoteWing, mountViewTools,
} from 'nabi-note'

const selected = [
  subscriptWing,
  linkWing,
  footnoteWing,
]
const locale = createLocale('zh')
const { nabi, registry } = createNabiWith(selected, { locale })

const root = document.querySelector('.nabi')!
const content = document.querySelector('.nabi-content')!

// The locale also sets the direction — Arabic and Urdu run right to left
mountSurface({ nabi, registry, root: content, locale })

const settle = watchSettle(document, { surface: content })
const shared = { nabi, registry, surface: content, settle, locale }
const toolbar = mountToolbar({ ...shared, root: document.querySelector('#toolbar')! })
const context = mountContextToolbar({ ...shared, root: document.querySelector('#context')! })
mountHints({ toolbar, context, root, surface: content })
// The preview and fullscreen buttons — they stand their own box at the end of the row
mountViewTools({ ...shared, root, container: document.querySelector('#toolbar')! })

// on every change — hook up your own code here
// nabi.onChange(() => user_callback(nabi.getHtml()))

// Call from your language selector. Content and editing history stay intact.
// locale.setLocale('ko')
ts
const selected = wings().use('a').build()

粘贴 URL ​

如果去掉首尾空白后的全部粘贴内容是有效的 http:// 或 https:// URL,就会立即作为链接插入。句子中的 URL、包含多个 URL 的文本以及相对路径仍按现有粘贴方式处理。在代码块或未启用链接 wing 的编辑器中,内容保持为文本。从编辑器复制的有效内部剪贴板数据优先处理,以保留原有格式。

可以通过 mountSurface() 的 onPasteUrl 回调设置链接的显示文字。

参数值
url去掉首尾空白后的完整 URL
isSameOriginURL 的协议、主机和端口是否与编辑器所在文档的源相同
path以 / 开头的路径,包含查询字符串和片段。例如:/posts/1?a=2#top
ts
const surface = mountSurface({
  nabi,
  registry,
  root: document.querySelector<HTMLElement>('#editor')!,
  onPasteUrl(url, isSameOrigin, path) {
    if (isSameOrigin && path === '/guide') return 'NABI NOTE'
    return url
  },
})

返回包含非空白字符的字符串时,该字符串会作为显示文字。如果没有回调,或回调返回空值、非字符串值或发生错误,则继续显示粘贴的 URL。返回的字符串不会被解释为 HTML,也不会改变链接地址。

回调也支持 async 函数和 Promise。等待响应时会立即插入 URL 链接;之后收到有效结果时,只更新该链接的显示文字。如果期间链接已被修改或删除、加载了新文档,或 surface 已卸载,则忽略迟到的结果。

CSS 样式 ​

普通链接用 .nabi-content a 设置样式,附件链接用 .nabi-content a[data-nabi-file] 另行设置。

css
.article-body a:not([data-nabi-file]) {
  color: var(--nabi-accent);
  text-decoration-thickness: .08em;
  text-underline-offset: .16em;
}

.article-body a[data-nabi-file] {
  display: inline-flex;
  gap: .35em;
  padding: .25em .55em;
  background: var(--nabi-soft);
}

附件链接的 ::before 和 ::after 用来显示文件图标和扩展名,所以通常不要替换或移除它们的 content。