3.0.0 • Published 4 years ago

@linkage-community/bottlemail v3.0.0

Weekly downloads
118
License
MPL-2.0
Repository
github
Last release
4 years ago

@linkage-community/bottlemail npm version codecov

これはなに

文字列からメンション (@dolphin), ウェブサイト (https://github.com), 絵文字名 (:emoji:) を取りだすためのライブラリ

使いかた

import parse, { NodeType } from '@linkage-community/bottlemail'
const nodes: NodeType[] = parse('@otofune Yo! :smile: https://github.com/ http://[fe80::a1b3:125d:c1f8:4780]/ @ @test')
console.dir(nodes)
/*
[ { kind: 'Mention', raw: '@otofune', value: 'otofune' },
  { kind: 'Text', raw: ' Yo! ', value: ' Yo! ' },
  { kind: 'EmojiName', raw: ':smile:', value: 'smile' },
  { kind: 'Text', raw: ' ', value: ' ' },
  { kind: 'Link',
    raw: 'https://github.com/',
    value: 'https://github.com/' },
  { kind: 'Text', raw: ' ', value: ' ' },
  { kind: 'Link',
    raw: 'http://[fe80::a1b3:125d:c1f8:4780]/',
    value: 'http://[fe80::a1b3:125d:c1f8:4780]/' },
  { kind: 'Text', raw: ' @ ', value: ' @ ' },
  { kind: 'Mention', raw: '@test', value: 'test' } ]
*/

記法について

詳しくは src/parser.ts を参照するとよい

  • 開始文字によって Kind の判定が始まる
  • \ (バックスラッシュ) があると次の文字の開始文字判定がスキップされる

NodeType について

詳しくは src/types.ts を参照するとよい

type NodeType = {
  kind: "EmojiName" | "Mention" | "Link" | "Text",
  value: string,
  raw: string,
}

種類kindrawvalue
絵文字名EmojiName:smile:smile
メンションMention@dolphindolphin
テキストTextyee hawyee haw
ウェブサイトLinkhttps://github.comhttps://github.com

Type Guard

is${kind} というヘルパ関数があるので、これを使うとよい。 src/types.ts に定義されているが、src/index.ts から re-export されているため、直接 import できる

想定 Q&A

空文字列 ("") のときはどのような結果になりますか?

空配列になる

例外は発生しますか?

発生しない。文字列でないものが与えられた場合、空配列になる

Link Kind では、node.rawnode.value に違いはありますか?

URL として無効な文字列を除去したものが入る。
現在は iOS でコピーアンドペーストすると稀に発生することがある、末尾についた ZWSP (ゼロ幅スペース) を除去する。

3.0.0

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

5 years ago

1.0.0

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago