2md 中文文档教程
2md: convert formatted text to markdown
有很多用于将 markdown 转换为 html 的包,但是这个 反过来,将格式化的 html 转换为 markdown。
假设您正在阅读 Ars Technica 文章 并想复制一些东西 进入一些降价笔记。 只需在浏览器中选择一些内容,复制 它,然后运行 2md
。 标题、列表格式、粗体文本和超链接 都保存下来了。
您可以在 2md.ca 在线试用。
存在内部结构的详细文章:“Compiling HTML to Markdown 使用 TypeScript:2md 的工作原理” 从命令行
Installation
尝试 2md
的最简单方法是使用 [npx
][],a 自动下载、缓存和运行程序的工具; 它已包含在内 使用 Node.js 自 2017 年以来:
npx 2md [--no-quote] [FILE]
您还可以安装 <带有 yarn
的 code>2md 命令:
yarn [global] add 2md
Usage
运行
npx 2md [--no-quote] [FILE]
以获取 markdown。
默认情况下,2md 从剪贴板读取,使用 osascript
, [xclip
][], 或 powershell
。 否则,将 html 文件的名称作为命令行传递给它 争论。
为了方便地将内容插入到其他文档中,--quote
由 默认并将 markdown 包装在块引用中:
> # Foo
>
> bar ...
API
仅公开一个函数:toMd
。
const { toMd } = require('2md');
console.log(toMd('foo <b>bar</b>'));
仅
foo **bar**
支持路径中带有 public
的导出文件。 一切 其他内容如有更改,恕不另行通知。 但如果有一些有趣的 这里的代码你想重用,让我知道,我可以考虑发布 它作为一个单独的包。
Contributing
欢迎投稿! 有相当全面的端到端和 往返测试,TypeScript 的类型检查使重构更安全, 所以不要害怕移动代码。
License
这里所有的原始代码都是在 Apache License 下获得许可的,版本 2.0,包含在LICENSE.code
中; 除了“如何 works”文章how-it-works/post.mdx
,它是不可再分发的。
Releasing
当前的发布过程,稍后将被自动化,是:
从
core/package.json
中的version
字段中删除-pre
标签version
,并从website/package.json
中的2md
依赖更新
CHANGELOG.md
Copy
README .md
和任何引用的图像,例如doc/demo.gif
到core
文件夹提交git,并
在
git tag vA.BC
core
目录下,yarn run package
和检查 tarballgit push --atomic $REMOTE main vA.BC
可选:找出将
CHANGELOG.md
摘录放入的自动化 自动创建 [GitHub 发布][gh-release]。[gh-release]: https://github.com/andrewdotn/2md/releases
npm publish 2md-vA.BCtgz
如果发布预发布,添加
npm publish --tag next
到 [设置 正确的 npm 标签][npm-next]。[npm-next]: https://medium.com/@mbostock/prereleases-and-npm-e778fc5e2420
跳转版本并在
core/package.json-pre
版本后缀代码>; 更新website/package.json
中的2md
依赖版本Otherwise yarn won’t use the local version.
纱线工作区文档说,
if workspace-b depends on a different version than the one referenced in workspace-a’s package.json, the dependency will be installed from npm rather than linked from your local filesystem. This is because some packages actually need to use the previous versions in order to build the new ones (Babel is one of them).
对于预编译版本,在
core
中运行yarn dist
并复制2md.js
到~/bin
。 它依赖于一个外壳包装器:#!/bin/bash DIR="$(dirname -- "${0}")" exec node "${DIR}/2md.js" "${@}"
2md: convert formatted text to markdown
There are lots of packages for turning markdown into html, but this one goes the other way, turning formatted html into markdown.
Say you’re reading an Ars Technica article and want to copy something into some markdown notes. Just select some content in your browser, copy it, then run 2md
. The heading, list formatting, bold text, and hyperlinks are all preserved.
You can try it out online at 2md.ca.
A detailed writeup of the internals exists: “Compiling HTML to Markdown with TypeScript: How 2md works”
Installation
The easiest way to try out 2md
from the command line is with [npx
][], a tool to automatically download, cache, and run programs; it’s been included with Node.js since 2017:
npx 2md [--no-quote] [FILE]
You can also install to install the 2md
command with yarn
:
yarn [global] add 2md
Usage
Run
npx 2md [--no-quote] [FILE]
to get markdown.
By default, 2md reads from the clipboard, using osascript
, [xclip
][], or powershell
. Otherwise, pass it the name of html file as a command-line argument.
For easy inserting of stuff into other documents, --quote
is on by default and wraps the markdown in a blockquote:
> # Foo
>
> bar ...
API
Only a single function is exposed: toMd
.
const { toMd } = require('2md');
console.log(toMd('foo <b>bar</b>'));
prints
foo **bar**
Only exported files with public
in the path are supported. Everything else is subject to change without notice. But if there’s some interesting code here you’d like to reuse, let me know and I can look into publishing it as a separate package.
Contributing
Contributions are welcome! There are fairly comprehensive end-to-end and round-trip tests, and TypeScript’s type-checking makes refactoring safer, so don’t be afraid to move code around.
License
All the original code here is licensed under the Apache License, version 2.0, included in LICENSE.code
; except for the contents of the “how it works” article how-it-works/post.mdx
, which is not redistributable.
Releasing
The current release process, to be automated later, is:
Remove the
-pre
tag from theversion
field incore/package.json
version
, and from the2md
dependency inwebsite/package.json
Update
CHANGELOG.md
Copy
README.md
and any referenced images such asdoc/demo.gif
into thecore
folderCommit to git, and
git tag vA.B.C
In the
core
directory,yarn run package
and inspect tarballgit push --atomic $REMOTE main vA.B.C
Optional: figure out automation to put
CHANGELOG.md
excerpt into auto-created [GitHub releases][gh-release].[gh-release]: https://github.com/andrewdotn/2md/releases
npm publish 2md-vA.B.C.tgz
If publishing a pre-release, add
npm publish --tag next
to [set the correct npm tag][npm-next].[npm-next]: https://medium.com/@mbostock/prereleases-and-npm-e778fc5e2420
Bump version and add
-pre
version suffix incore/package.json
; update the2md
dependency version inwebsite/package.json
as wellOtherwise yarn won’t use the local version.
The <a href="https://classic.yarnpkg.com/en/docs/workspaces/"
yarn workspaces documentation says,
if workspace-b depends on a different version than the one referenced in workspace-a’s package.json, the dependency will be installed from npm rather than linked from your local filesystem. This is because some packages actually need to use the previous versions in order to build the new ones (Babel is one of them).
For the precompiled version, run
yarn dist
incore
and copy2md.js
to~/bin
. It relies on a shell wrapper:#!/bin/bash DIR="$(dirname -- "${0}")" exec node "${DIR}/2md.js" "${@}"