applesauce-content 0.0.0-next-20241111095753 → 0.0.0-next-20241113103021
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 +1 -0
- package/dist/helpers/regexp.js +3 -0
- package/dist/nast/eol-metadata.js +3 -1
- package/dist/text/content.d.ts +3 -3
- package/dist/text/content.js +15 -5
- package/dist/text/emoji.d.ts +1 -1
- package/dist/text/index.d.ts +1 -0
- package/dist/text/index.js +1 -0
- package/dist/text/link.d.ts +3 -0
- package/dist/text/link.js +22 -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 +2 -3
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
|
@@ -3,7 +3,9 @@ export function eolMetadata() {
|
|
|
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 (
|
|
6
|
+
if ((node.type === "text" && node.value.endsWith("\n")) ||
|
|
7
|
+
!next ||
|
|
8
|
+
(next.type === "text" && next.value.startsWith("\n"))) {
|
|
7
9
|
node.data = node.data || {};
|
|
8
10
|
node.data.eol = true;
|
|
9
11
|
}
|
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
|
@@ -8,9 +8,11 @@ import { hashtags } from "./hashtag.js";
|
|
|
8
8
|
import { galleries } from "./gallery.js";
|
|
9
9
|
import { lightningInvoices } from "./lightning.js";
|
|
10
10
|
import { eolMetadata } from "../nast/eol-metadata.js";
|
|
11
|
-
|
|
11
|
+
import { links } from "./links.js";
|
|
12
|
+
export const TextNoteContentSymbol = Symbol.for("text-note-content");
|
|
12
13
|
// default kind 1 transformers
|
|
13
|
-
export const
|
|
14
|
+
export const textNoteTransformers = [
|
|
15
|
+
links,
|
|
14
16
|
nostrMentions,
|
|
15
17
|
galleries,
|
|
16
18
|
emojis,
|
|
@@ -20,7 +22,7 @@ export const defaultTransformers = [
|
|
|
20
22
|
eolMetadata,
|
|
21
23
|
];
|
|
22
24
|
/** Parsed and process a note with custom transformers */
|
|
23
|
-
export function
|
|
25
|
+
export function getParsedContent(event, content, transformers = textNoteTransformers, cacheKey = TextNoteContentSymbol) {
|
|
24
26
|
// process strings
|
|
25
27
|
if (typeof event === "string") {
|
|
26
28
|
const processor = unified();
|
|
@@ -29,7 +31,15 @@ export function getParsedTextContent(event, content, transformers = defaultTrans
|
|
|
29
31
|
}
|
|
30
32
|
return processor.runSync(createTextNoteATS(event, content));
|
|
31
33
|
}
|
|
32
|
-
|
|
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, () => {
|
|
33
43
|
const processor = unified();
|
|
34
44
|
for (const transformer of transformers) {
|
|
35
45
|
processor.use(transformer);
|
|
@@ -39,5 +49,5 @@ export function getParsedTextContent(event, content, transformers = defaultTrans
|
|
|
39
49
|
}
|
|
40
50
|
export function removeParsedTextContent(event) {
|
|
41
51
|
// @ts-expect-error
|
|
42
|
-
delete event[
|
|
52
|
+
delete event[TextNoteContentSymbol];
|
|
43
53
|
}
|
package/dist/text/emoji.d.ts
CHANGED
package/dist/text/index.d.ts
CHANGED
package/dist/text/index.js
CHANGED
|
@@ -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
|
+
}
|
|
@@ -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.0.0-next-
|
|
3
|
+
"version": "0.0.0-next-20241113103021",
|
|
4
4
|
"description": "Unified plugins for processing event content",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -40,8 +40,7 @@
|
|
|
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.0.0-next-
|
|
44
|
-
"linkifyjs": "^4.1.3",
|
|
43
|
+
"applesauce-core": "0.0.0-next-20241113103021",
|
|
45
44
|
"mdast-util-find-and-replace": "^3.0.1",
|
|
46
45
|
"nostr-tools": "^2.10.1",
|
|
47
46
|
"remark": "^15.0.1",
|