-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8ae9315
Showing
16 changed files
with
491 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.dnt-import-map.json | ||
npm/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"recommendations": [ | ||
"denoland.vscode-deno", | ||
"streetsidesoftware.code-spell-checker" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"deno.enable": true, | ||
"deno.unstable": true, | ||
"files.eol": "\n", | ||
"files.insertFinalNewline": true, | ||
"files.trimFinalNewlines": true, | ||
"[json]": { | ||
"editor.defaultFormatter": "vscode.json-language-features", | ||
"editor.formatOnSave": true | ||
}, | ||
"[jsonc]": { | ||
"editor.defaultFormatter": "vscode.json-language-features", | ||
"editor.formatOnSave": true | ||
}, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno", | ||
"editor.formatOnSave": true, | ||
"editor.codeActionsOnSave": { | ||
"source.sortImports": "always" | ||
} | ||
}, | ||
"cSpell.words": [ | ||
"markdownit" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"deno": { | ||
"enable": true | ||
}, | ||
"ensure_final_newline_on_save": true, | ||
"format_on_save": "on", | ||
"formatter": "language_server", | ||
"languages": { | ||
"TypeScript": { | ||
"language_servers": [ | ||
"deno", | ||
"!typescript-language-server", | ||
"!eslint", | ||
"..." | ||
] | ||
}, | ||
"TSX": { | ||
"language_servers": [ | ||
"deno", | ||
"!typescript-language-server", | ||
"!eslint", | ||
"..." | ||
] | ||
} | ||
}, | ||
"show_wrap_guides": true, | ||
"wrap_guides": [80] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!-- deno-fmt-ignore-file --> | ||
|
||
Changelog | ||
========= | ||
|
||
Version 0.1.0 | ||
------------- | ||
|
||
To be released. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
MIT License | ||
|
||
Copyright 2024 Hong Minhee | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<!-- deno-fmt-ignore-file --> | ||
|
||
@fedify/markdown-it-hashtag | ||
=========================== | ||
|
||
This is a [markdown-it] plugin that parses and renders Mastodon-style #hashtags. | ||
It converts, for example, `#FooBar` into a link: | ||
|
||
~~~~ html | ||
<a href="#FooBar"><span class="hash">#</span><span class="tag">FooBar</span></a> | ||
~~~~ | ||
|
||
The value of `href` attributes, other attributes (if any), and the content of | ||
the link can be customized by passing options to the plugin: | ||
|
||
~~~~ typescript | ||
import MarkdownIt from "markdown-it"; | ||
import { hashtag, spanHashAndTag } from "@fedify/markdown-it-hashtag"; | ||
|
||
const md = new MarkdownIt(); | ||
md.use(hashtag, { | ||
link: (tag: string) => `https://example.com/tags/${tag.substring(1)}`, | ||
linkAttributes: (handle: string) => ({ class: "hashtag" }), | ||
label: spanHashAndTag, | ||
}); | ||
~~~~ | ||
|
||
If you want to collect all hashtags in a document, you can pass an environment | ||
object to the `render()` method: | ||
|
||
~~~~ typescript | ||
const env = {}; | ||
md.render( | ||
"Hello, #FooBar\n\n> #BazQux", | ||
env, | ||
); | ||
console.log(env.hashtags); // ["#FooBar", "#BazQux"] | ||
~~~~ | ||
|
||
[markdown-it]: https://github.com/markdown-it/markdown-it | ||
|
||
|
||
Installation | ||
------------ | ||
|
||
### Deno | ||
|
||
~~~~ sh | ||
deno add @fedify/markdown-it-hashtag | ||
~~~~ | ||
|
||
### Node.js | ||
|
||
~~~~ sh | ||
npm add @fedify/markdown-it-hashtag | ||
~~~~ | ||
|
||
### Bun | ||
|
||
~~~~ sh | ||
bun add @fedify/markdown-it-hashtag | ||
~~~~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@fedify/markdown-it-hashtag", | ||
"version": "0.1.0", | ||
"exports": "./mod.ts", | ||
"imports": { | ||
"@deno/dnt": "jsr:@deno/dnt@^0.41.1", | ||
"@std/assert": "jsr:@std/assert@^0.225.1", | ||
"@std/html": "jsr:@std/html@^0.224.0", | ||
"markdown-it": "npm:@types/markdown-it@^14.1.1", | ||
"markdown-it-impl": "npm:markdown-it@^14.1.0" | ||
}, | ||
"exclude": [ | ||
".dnt-import-map.json", | ||
"npm/" | ||
], | ||
"lock": false, | ||
"tasks": { | ||
"check": "deno check **/*.ts && deno lint && deno fmt --check", | ||
"dnt": "deno run --allow-all dnt.ts", | ||
"hooks:install": "deno run --allow-read=deno.json,.git/hooks/ --allow-write=.git/hooks/ jsr:@hongminhee/deno-task-hooks", | ||
"hooks:pre-commit": "deno task check", | ||
"hooks:pre-push": "deno test" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { build, emptyDir } from "@deno/dnt"; | ||
import metadata from "./deno.json" with { type: "json" }; | ||
|
||
await emptyDir("./npm"); | ||
|
||
const importMap = ".dnt-import-map.json"; | ||
await Deno.writeTextFile( | ||
importMap, | ||
JSON.stringify({ | ||
imports: { | ||
...metadata.imports, | ||
"markdown-it": metadata.imports["markdown-it-impl"], | ||
"markdown-it/": `npm:/${ | ||
metadata.imports["markdown-it-impl"].substring(4) | ||
}/`, | ||
}, | ||
}), | ||
); | ||
|
||
await build({ | ||
package: { | ||
// package.json properties | ||
name: "@fedify/markdown-it-hashtag", | ||
version: Deno.args[0] ?? metadata.version, | ||
description: | ||
"A markdown-it plugin that parses and renders Mastodon-style #hashtags.", | ||
keywords: [ | ||
"markdown", | ||
"markdown-it", | ||
"markdown-it-plugin", | ||
"Mastodon", | ||
"hashtag", | ||
"fediverse", | ||
], | ||
license: "MIT", | ||
author: { | ||
name: "Hong Minhee", | ||
email: "[email protected]", | ||
url: "https://hongminhee.org/", | ||
}, | ||
homepage: "https://github.com/dahlia/markdown-it-hashtag", | ||
repository: { | ||
type: "git", | ||
url: "git+https://github.com/dahlia/markdown-it-hashtag.git", | ||
}, | ||
bugs: { | ||
url: "https://github.com/dahlia/markdown-it-hashtag/issues", | ||
}, | ||
devDependencies: { | ||
"@types/markdown-it": "^14.1.1", | ||
}, | ||
}, | ||
outDir: "./npm", | ||
entryPoints: ["./mod.ts"], | ||
importMap, | ||
shims: { | ||
deno: true, | ||
}, | ||
typeCheck: "both", | ||
declaration: "separate", | ||
declarationMap: true, | ||
test: true, | ||
async postBuild() { | ||
await Deno.copyFile("LICENSE", "npm/LICENSE"); | ||
await Deno.copyFile("README.md", "npm/README.md"); | ||
}, | ||
}); | ||
|
||
// cSpell: ignore Minhee |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from "./src/mod.ts"; | ||
import { hashtag } from "./src/mod.ts"; | ||
|
||
export default hashtag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { isLinkClose, isLinkOpen } from "./html.ts"; | ||
import { assert } from "@std/assert/assert"; | ||
import { assertFalse } from "@std/assert/assert-false"; | ||
|
||
Deno.test("isLinkOpen()", () => { | ||
assert(isLinkOpen('<a href="https://example.com/">')); | ||
assert(isLinkOpen("<a>")); | ||
assertFalse(isLinkOpen("</a>")); | ||
assertFalse(isLinkOpen("<b>")); | ||
assertFalse(isLinkOpen("</b>")); | ||
assertFalse(isLinkOpen("asdf")); | ||
}); | ||
|
||
Deno.test("isLinkClose()", () => { | ||
assert(isLinkClose("</a>")); | ||
assert(isLinkClose("</a >")); | ||
assertFalse(isLinkClose("<a>")); | ||
assertFalse(isLinkClose("<b>")); | ||
assertFalse(isLinkClose("</b>")); | ||
assertFalse(isLinkClose("asdf")); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const LINK_OPEN_PATTERN = /^<a[>\s]/i; | ||
const LINK_CLOSE_PATTERN = /^<\/a\s*>/i; | ||
|
||
/** | ||
* Tests if the given string is an opening link tag. | ||
* @function | ||
* @param html The string to test. | ||
* @returns `true` if the given string is an opening link tag, | ||
* or `false` otherwise. | ||
*/ | ||
export const isLinkOpen = LINK_OPEN_PATTERN.test.bind(LINK_OPEN_PATTERN); | ||
|
||
/** | ||
* Tests if the given string is a closing link tag. | ||
* @function | ||
* @param html The string to test. | ||
* @returns `true` if the given string is a closing link tag, | ||
* or `false` otherwise. | ||
*/ | ||
export const isLinkClose = LINK_CLOSE_PATTERN.test.bind(LINK_CLOSE_PATTERN); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { escape } from "@std/html"; | ||
|
||
/** | ||
* Renders a hashtag into an HTML string. | ||
*/ | ||
export function spanHashAndTag(hashtag: string): string { | ||
if (hashtag.startsWith("#")) { | ||
hashtag = hashtag.substring(1); | ||
} | ||
return `<span class="hash">#</span><span class="tag">${ | ||
escape(hashtag) | ||
}</span>`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./label.ts"; | ||
export * from "./plugin.ts"; | ||
import { hashtag } from "./plugin.ts"; | ||
|
||
export default hashtag; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { assertEquals } from "@std/assert/assert-equals"; | ||
import MarkdownIt from "markdown-it-impl"; | ||
import { hashtag } from "./plugin.ts"; | ||
|
||
Deno.test("hashtag()", () => { | ||
const md = new MarkdownIt({ | ||
html: true, | ||
}); | ||
md.use(hashtag); | ||
// deno-lint-ignore no-explicit-any | ||
const env: any = {}; | ||
const html = md.render( | ||
`\ | ||
**Hello**, *#FooBar*! | ||
> #BazQux | ||
[This should be ignored: #FooBar](https://example.com/) | ||
<a href="">This also should be ignored: #FooBar</a> | ||
`, | ||
env, | ||
); | ||
assertEquals(env.hashtags, [ | ||
"#FooBar", | ||
"#BazQux", | ||
]); | ||
assertEquals( | ||
html, | ||
`\ | ||
<p><strong>Hello</strong>, <em><a href="#FooBar"><span class="hash">#</span><span class="tag">FooBar</span></a></em>!</p> | ||
<blockquote> | ||
<p><a href="#BazQux"><span class="hash">#</span><span class="tag">BazQux</span></a></p> | ||
</blockquote> | ||
<p><a href="https://example.com/">This should be ignored: #FooBar</a></p> | ||
<p><a href="">This also should be ignored: #FooBar</a></p> | ||
`, | ||
); | ||
}); |
Oops, something went wrong.