applesauce-content 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/helpers/regexp.d.ts +2 -0
- package/dist/helpers/regexp.js +7 -1
- package/dist/nast/eol-metadata.js +6 -4
- package/dist/nast/types.d.ts +2 -0
- package/dist/text/content.d.ts +3 -3
- package/dist/text/content.js +25 -5
- package/dist/text/emoji.d.ts +1 -1
- package/dist/text/index.d.ts +2 -0
- package/dist/text/index.js +2 -0
- package/dist/text/lightning.d.ts +3 -0
- package/dist/text/lightning.js +25 -0
- package/dist/text/links.d.ts +3 -0
- package/dist/text/links.js +22 -0
- package/dist/text/parser.js +6 -8
- package/package.json +11 -18
package/README.md
CHANGED
|
@@ -5,13 +5,13 @@ applesauce package for parsing text note content
|
|
|
5
5
|
## Example
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
|
-
import {
|
|
8
|
+
import { getParsedContent } from "applesauce-content/text";
|
|
9
9
|
|
|
10
10
|
const stringContent = `
|
|
11
11
|
hello nostr!
|
|
12
12
|
nostr:npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6
|
|
13
13
|
`;
|
|
14
|
-
const ats =
|
|
14
|
+
const ats = getParsedContent(stringContent);
|
|
15
15
|
|
|
16
16
|
console.log(ats);
|
|
17
17
|
/*
|
package/dist/helpers/regexp.d.ts
CHANGED
package/dist/helpers/regexp.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
export const Expressions = {
|
|
2
|
+
get link() {
|
|
3
|
+
return /https?:\/\/([a-zA-Z0-9\.\-]+\.[a-zA-Z]+(?::\d+)?)([\/\?#][\p{L}\p{N}\p{M}&\.-\/\?=#\-@%\+_,:!~*]*)?/gu;
|
|
4
|
+
},
|
|
2
5
|
get cashu() {
|
|
3
6
|
return /(cashu(?:A|B)[A-Za-z0-9_-]{100,10000}={0,3})/gi;
|
|
4
7
|
},
|
|
5
8
|
get nostrLink() {
|
|
6
|
-
return /
|
|
9
|
+
return /nostr:((npub|note|nprofile|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,})/gi;
|
|
7
10
|
},
|
|
8
11
|
get emoji() {
|
|
9
12
|
return /:([a-zA-Z0-9_-]+):/gi;
|
|
@@ -11,5 +14,8 @@ export const Expressions = {
|
|
|
11
14
|
get hashtag() {
|
|
12
15
|
return /(?<=^|[^\p{L}#])#([\p{L}\p{N}\p{M}]+)/gu;
|
|
13
16
|
},
|
|
17
|
+
get lightning() {
|
|
18
|
+
return /(?:lightning:)?(LNBC[A-Za-z0-9]+)/gim;
|
|
19
|
+
},
|
|
14
20
|
};
|
|
15
21
|
export default Expressions;
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
export function eolMetadata() {
|
|
2
2
|
return (tree) => {
|
|
3
3
|
for (let i = 0; i < tree.children.length; i++) {
|
|
4
|
-
|
|
4
|
+
const node = tree.children[i];
|
|
5
5
|
const next = tree.children[i + 1];
|
|
6
|
-
if (
|
|
7
|
-
next
|
|
8
|
-
next.
|
|
6
|
+
if ((node.type === "text" && node.value.endsWith("\n")) ||
|
|
7
|
+
!next ||
|
|
8
|
+
(next.type === "text" && next.value.startsWith("\n"))) {
|
|
9
|
+
node.data = node.data || {};
|
|
10
|
+
node.data.eol = true;
|
|
9
11
|
}
|
|
10
12
|
}
|
|
11
13
|
};
|
package/dist/nast/types.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
|
2
2
|
import { DecodeResult } from "nostr-tools/nip19";
|
|
3
3
|
import { Node as UnistNode, Parent } from "unist";
|
|
4
4
|
import { type Token } from "@cashu/cashu-ts";
|
|
5
|
+
import { type ParsedInvoice } from "applesauce-core/helpers/bolt11";
|
|
5
6
|
export interface CommonData {
|
|
6
7
|
eol?: boolean;
|
|
7
8
|
}
|
|
@@ -34,6 +35,7 @@ export interface CashuToken extends Node {
|
|
|
34
35
|
export interface LightningInvoice extends Node {
|
|
35
36
|
type: "lightning";
|
|
36
37
|
invoice: string;
|
|
38
|
+
parsed: ParsedInvoice;
|
|
37
39
|
}
|
|
38
40
|
export interface Hashtag extends Node {
|
|
39
41
|
type: "hashtag";
|
package/dist/text/content.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { Transformer } from "unified";
|
|
|
2
2
|
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
3
3
|
import { Root } from "../nast/types.js";
|
|
4
4
|
import { galleries } from "./gallery.js";
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
5
|
+
export declare const TextNoteContentSymbol: unique symbol;
|
|
6
|
+
export declare const textNoteTransformers: (typeof galleries)[];
|
|
7
7
|
/** Parsed and process a note with custom transformers */
|
|
8
|
-
export declare function
|
|
8
|
+
export declare function getParsedContent(event: NostrEvent | EventTemplate | string, content?: string, transformers?: (() => Transformer<Root>)[], cacheKey?: symbol | null | undefined): Root;
|
|
9
9
|
export declare function removeParsedTextContent(event: NostrEvent | EventTemplate): void;
|
package/dist/text/content.js
CHANGED
|
@@ -6,11 +6,23 @@ import { emojis } from "./emoji.js";
|
|
|
6
6
|
import { createTextNoteATS } from "./parser.js";
|
|
7
7
|
import { hashtags } from "./hashtag.js";
|
|
8
8
|
import { galleries } from "./gallery.js";
|
|
9
|
-
|
|
9
|
+
import { lightningInvoices } from "./lightning.js";
|
|
10
|
+
import { eolMetadata } from "../nast/eol-metadata.js";
|
|
11
|
+
import { links } from "./links.js";
|
|
12
|
+
export const TextNoteContentSymbol = Symbol.for("text-note-content");
|
|
10
13
|
// default kind 1 transformers
|
|
11
|
-
export const
|
|
14
|
+
export const textNoteTransformers = [
|
|
15
|
+
links,
|
|
16
|
+
nostrMentions,
|
|
17
|
+
galleries,
|
|
18
|
+
emojis,
|
|
19
|
+
hashtags,
|
|
20
|
+
lightningInvoices,
|
|
21
|
+
cashuTokens,
|
|
22
|
+
eolMetadata,
|
|
23
|
+
];
|
|
12
24
|
/** Parsed and process a note with custom transformers */
|
|
13
|
-
export function
|
|
25
|
+
export function getParsedContent(event, content, transformers = textNoteTransformers, cacheKey = TextNoteContentSymbol) {
|
|
14
26
|
// process strings
|
|
15
27
|
if (typeof event === "string") {
|
|
16
28
|
const processor = unified();
|
|
@@ -19,7 +31,15 @@ export function getParsedTextContent(event, content, transformers = defaultTrans
|
|
|
19
31
|
}
|
|
20
32
|
return processor.runSync(createTextNoteATS(event, content));
|
|
21
33
|
}
|
|
22
|
-
|
|
34
|
+
// no caching
|
|
35
|
+
if (!cacheKey) {
|
|
36
|
+
const processor = unified();
|
|
37
|
+
for (const transformer of transformers) {
|
|
38
|
+
processor.use(transformer);
|
|
39
|
+
}
|
|
40
|
+
return processor.runSync(createTextNoteATS(event, content));
|
|
41
|
+
}
|
|
42
|
+
return getOrComputeCachedValue(event, cacheKey, () => {
|
|
23
43
|
const processor = unified();
|
|
24
44
|
for (const transformer of transformers) {
|
|
25
45
|
processor.use(transformer);
|
|
@@ -29,5 +49,5 @@ export function getParsedTextContent(event, content, transformers = defaultTrans
|
|
|
29
49
|
}
|
|
30
50
|
export function removeParsedTextContent(event) {
|
|
31
51
|
// @ts-expect-error
|
|
32
|
-
delete event[
|
|
52
|
+
delete event[TextNoteContentSymbol];
|
|
33
53
|
}
|
package/dist/text/emoji.d.ts
CHANGED
package/dist/text/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "./content.js";
|
|
2
|
+
export * from "./links.js";
|
|
2
3
|
export * from "./mentions.js";
|
|
3
4
|
export * from "./cashu.js";
|
|
4
5
|
export * from "./emoji.js";
|
|
5
6
|
export * from "./parser.js";
|
|
6
7
|
export * from "./hashtag.js";
|
|
7
8
|
export * from "./gallery.js";
|
|
9
|
+
export * from "./lightning.js";
|
package/dist/text/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "./content.js";
|
|
2
|
+
export * from "./links.js";
|
|
2
3
|
export * from "./mentions.js";
|
|
3
4
|
export * from "./cashu.js";
|
|
4
5
|
export * from "./emoji.js";
|
|
5
6
|
export * from "./parser.js";
|
|
6
7
|
export * from "./hashtag.js";
|
|
7
8
|
export * from "./gallery.js";
|
|
9
|
+
export * from "./lightning.js";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { parseBolt11 } from "applesauce-core/helpers/bolt11";
|
|
2
|
+
import Expressions from "../helpers/regexp.js";
|
|
3
|
+
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
export function lightningInvoices() {
|
|
5
|
+
return (tree) => {
|
|
6
|
+
findAndReplace(tree, [
|
|
7
|
+
[
|
|
8
|
+
Expressions.lightning,
|
|
9
|
+
(_, $1) => {
|
|
10
|
+
try {
|
|
11
|
+
const invoice = $1;
|
|
12
|
+
const parsed = parseBolt11(invoice);
|
|
13
|
+
return {
|
|
14
|
+
type: "lightning",
|
|
15
|
+
invoice,
|
|
16
|
+
parsed,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
catch (error) { }
|
|
20
|
+
return false;
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
]);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Expressions from "../helpers/regexp.js";
|
|
2
|
+
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
3
|
+
export function links() {
|
|
4
|
+
return (tree) => {
|
|
5
|
+
findAndReplace(tree, [
|
|
6
|
+
[
|
|
7
|
+
Expressions.link,
|
|
8
|
+
(_) => {
|
|
9
|
+
try {
|
|
10
|
+
return {
|
|
11
|
+
type: "link",
|
|
12
|
+
href: new URL(_).toString(),
|
|
13
|
+
value: _,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
catch (error) { }
|
|
17
|
+
return false;
|
|
18
|
+
},
|
|
19
|
+
],
|
|
20
|
+
]);
|
|
21
|
+
};
|
|
22
|
+
}
|
package/dist/text/parser.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import { tokenize } from "linkifyjs";
|
|
2
1
|
/** Creates a {@link Root} ATS node for a text note */
|
|
3
2
|
export function createTextNoteATS(event, content) {
|
|
4
|
-
const tokens = tokenize(content || (typeof event === "string" ? event : event.content));
|
|
5
3
|
return {
|
|
6
4
|
type: "root",
|
|
7
5
|
event: typeof event !== "string" ? event : undefined,
|
|
8
|
-
children:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
children: [
|
|
7
|
+
{
|
|
8
|
+
type: "text",
|
|
9
|
+
value: content || (typeof event === "string" ? event : event.content),
|
|
10
|
+
},
|
|
11
|
+
],
|
|
14
12
|
};
|
|
15
13
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-content",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "Unified plugins for processing event content",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -36,37 +36,30 @@
|
|
|
36
36
|
}
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@cashu/cashu-ts": "
|
|
39
|
+
"@cashu/cashu-ts": "2.0.0-rc1",
|
|
40
40
|
"@types/hast": "^3.0.4",
|
|
41
41
|
"@types/mdast": "^4.0.4",
|
|
42
42
|
"@types/unist": "^3.0.3",
|
|
43
|
-
"applesauce-core": "^0.
|
|
44
|
-
"linkifyjs": "^4.1.3",
|
|
43
|
+
"applesauce-core": "^0.10.0",
|
|
45
44
|
"mdast-util-find-and-replace": "^3.0.1",
|
|
46
|
-
"nostr-tools": "^2.
|
|
45
|
+
"nostr-tools": "^2.10.3",
|
|
47
46
|
"remark": "^15.0.1",
|
|
48
47
|
"remark-parse": "^11.0.0",
|
|
49
48
|
"unified": "^11.0.5",
|
|
50
49
|
"unist-util-visit-parents": "^6.0.1"
|
|
51
50
|
},
|
|
52
51
|
"devDependencies": {
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"jest": "^29.7.0",
|
|
56
|
-
"jest-extended": "^4.0.2"
|
|
52
|
+
"typescript": "^5.6.3",
|
|
53
|
+
"vitest": "^2.1.8"
|
|
57
54
|
},
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
|
|
61
|
-
],
|
|
62
|
-
"setupFilesAfterEnv": [
|
|
63
|
-
"jest-extended/all"
|
|
64
|
-
]
|
|
55
|
+
"funding": {
|
|
56
|
+
"type": "lightning",
|
|
57
|
+
"url": "lightning:nostrudel@geyser.fund"
|
|
65
58
|
},
|
|
66
59
|
"scripts": {
|
|
67
60
|
"build": "tsc",
|
|
68
61
|
"watch:build": "tsc --watch > /dev/null",
|
|
69
|
-
"test": "
|
|
70
|
-
"watch:test": "
|
|
62
|
+
"test": "vitest run --passWithNoTests",
|
|
63
|
+
"watch:test": "vitest"
|
|
71
64
|
}
|
|
72
65
|
}
|