applesauce-content 0.0.0-next-20250526151506 → 0.0.0-next-20250606170247
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/dist/__tests__/exports.test.d.ts +1 -0
- package/dist/__tests__/exports.test.js +14 -0
- package/dist/__tests__/fake-user.d.ts +9 -0
- package/dist/__tests__/fake-user.js +23 -0
- package/dist/nast/types.d.ts +2 -1
- package/dist/text/__tests__/exports.test.js +1 -0
- package/dist/text/__tests__/gallery.test.d.ts +1 -0
- package/dist/text/__tests__/gallery.test.js +38 -0
- package/dist/text/__tests__/hashtag.test.d.ts +1 -0
- package/dist/text/__tests__/hashtag.test.js +48 -0
- package/dist/text/content.js +4 -4
- package/dist/text/emoji.d.ts +1 -1
- package/dist/text/emoji.js +1 -1
- package/dist/text/gallery.d.ts +1 -1
- package/dist/text/gallery.js +1 -1
- package/dist/text/hashtag.d.ts +1 -0
- package/dist/text/hashtag.js +5 -5
- package/dist/text/lightning.d.ts +1 -0
- package/dist/text/lightning.js +1 -0
- package/dist/text/links.d.ts +1 -0
- package/dist/text/links.js +1 -0
- package/dist/text/mentions.d.ts +1 -0
- package/dist/text/mentions.js +1 -0
- package/dist/text/parser.d.ts +3 -1
- package/dist/text/parser.js +3 -1
- package/package.json +3 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import * as exports from "../index.js";
|
|
3
|
+
describe("exports", () => {
|
|
4
|
+
it("should export the expected functions", () => {
|
|
5
|
+
expect(Object.keys(exports).sort()).toMatchInlineSnapshot(`
|
|
6
|
+
[
|
|
7
|
+
"Helpers",
|
|
8
|
+
"Markdown",
|
|
9
|
+
"Nast",
|
|
10
|
+
"Text",
|
|
11
|
+
]
|
|
12
|
+
`);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
|
|
2
|
+
import type { EventTemplate, NostrEvent } from "nostr-tools";
|
|
3
|
+
export declare class FakeUser extends SimpleSigner {
|
|
4
|
+
pubkey: string;
|
|
5
|
+
event(data?: Partial<EventTemplate>): NostrEvent;
|
|
6
|
+
note(content?: string, extra?: Partial<EventTemplate>): import("nostr-tools").Event;
|
|
7
|
+
profile(profile: any, extra?: Partial<EventTemplate>): import("nostr-tools").Event;
|
|
8
|
+
contacts(pubkeys?: string[]): import("nostr-tools").Event;
|
|
9
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { unixNow } from "applesauce-core/helpers";
|
|
2
|
+
import { SimpleSigner } from "applesauce-signers/signers/simple-signer";
|
|
3
|
+
import { finalizeEvent, getPublicKey, kinds } from "nostr-tools";
|
|
4
|
+
export class FakeUser extends SimpleSigner {
|
|
5
|
+
pubkey = getPublicKey(this.key);
|
|
6
|
+
event(data) {
|
|
7
|
+
return finalizeEvent({
|
|
8
|
+
kind: data?.kind ?? kinds.ShortTextNote,
|
|
9
|
+
content: data?.content || "",
|
|
10
|
+
created_at: data?.created_at ?? unixNow(),
|
|
11
|
+
tags: data?.tags || [],
|
|
12
|
+
}, this.key);
|
|
13
|
+
}
|
|
14
|
+
note(content = "Hello World", extra) {
|
|
15
|
+
return this.event({ kind: kinds.ShortTextNote, content, ...extra });
|
|
16
|
+
}
|
|
17
|
+
profile(profile, extra) {
|
|
18
|
+
return this.event({ kind: kinds.Metadata, content: JSON.stringify({ ...profile }), ...extra });
|
|
19
|
+
}
|
|
20
|
+
contacts(pubkeys = []) {
|
|
21
|
+
return this.event({ kind: kinds.Contacts, tags: pubkeys.map((p) => ["p", p]) });
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/nast/types.d.ts
CHANGED
|
@@ -43,7 +43,8 @@ export interface Hashtag extends Node {
|
|
|
43
43
|
name: string;
|
|
44
44
|
/** The lowercase canonical name */
|
|
45
45
|
hashtag: string;
|
|
46
|
-
tag
|
|
46
|
+
/** The indexable tag for the hashtag. will be undefined if none was found */
|
|
47
|
+
tag?: ["t", ...string[]];
|
|
47
48
|
}
|
|
48
49
|
export interface Emoji extends Node {
|
|
49
50
|
type: "emoji";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getParsedContent } from "../content.js";
|
|
3
|
+
import { galleries } from "../gallery.js";
|
|
4
|
+
import { links } from "../links.js";
|
|
5
|
+
describe("gallery", () => {
|
|
6
|
+
it("should group image urls into galleries", () => {
|
|
7
|
+
expect(getParsedContent("Hello https://example.com/image.png https://example.com/image2.png", undefined, [
|
|
8
|
+
links,
|
|
9
|
+
galleries,
|
|
10
|
+
]).children).toEqual([
|
|
11
|
+
expect.objectContaining({ type: "text" }),
|
|
12
|
+
expect.objectContaining({
|
|
13
|
+
type: "gallery",
|
|
14
|
+
links: ["https://example.com/image.png", "https://example.com/image2.png"],
|
|
15
|
+
}),
|
|
16
|
+
]);
|
|
17
|
+
});
|
|
18
|
+
it("should not match a single image link", () => {
|
|
19
|
+
expect(getParsedContent("Hello https://example.com/image.png", undefined, [links, galleries]).children).toEqual([
|
|
20
|
+
expect.objectContaining({ type: "text" }),
|
|
21
|
+
expect.objectContaining({ type: "link" }),
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
it("should match multiple galleries", () => {
|
|
25
|
+
expect(getParsedContent("Hello https://example.com/image.png\nhttps://example.com/image2.png\n\nAnd here are the other images https://example.com/image3.png\n\nhttps://example.com/image4.png", undefined, [links, galleries]).children).toEqual([
|
|
26
|
+
expect.objectContaining({ type: "text" }),
|
|
27
|
+
expect.objectContaining({
|
|
28
|
+
type: "gallery",
|
|
29
|
+
links: ["https://example.com/image.png", "https://example.com/image2.png"],
|
|
30
|
+
}),
|
|
31
|
+
expect.objectContaining({ type: "text" }),
|
|
32
|
+
expect.objectContaining({
|
|
33
|
+
type: "gallery",
|
|
34
|
+
links: ["https://example.com/image3.png", "https://example.com/image4.png"],
|
|
35
|
+
}),
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { getParsedContent } from "../content.js";
|
|
3
|
+
import { hashtags } from "../hashtag.js";
|
|
4
|
+
import { FakeUser } from "../../__tests__/fake-user.js";
|
|
5
|
+
const user = new FakeUser();
|
|
6
|
+
describe("hashtags", () => {
|
|
7
|
+
it("should match all hashtags if event is missing", () => {
|
|
8
|
+
expect(getParsedContent("Hello #hashtag", undefined, [hashtags]).children).toMatchInlineSnapshot(`
|
|
9
|
+
[
|
|
10
|
+
{
|
|
11
|
+
"type": "text",
|
|
12
|
+
"value": "Hello ",
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
"hashtag": "hashtag",
|
|
16
|
+
"name": "hashtag",
|
|
17
|
+
"tag": undefined,
|
|
18
|
+
"type": "hashtag",
|
|
19
|
+
},
|
|
20
|
+
]
|
|
21
|
+
`);
|
|
22
|
+
});
|
|
23
|
+
describe("with event", () => {
|
|
24
|
+
it("should match all hashtags", () => {
|
|
25
|
+
expect(getParsedContent(user.event({ content: "Hello #hashtag", tags: [["t", "hashtag"]] }), undefined, [hashtags])
|
|
26
|
+
.children).toMatchInlineSnapshot(`
|
|
27
|
+
[
|
|
28
|
+
{
|
|
29
|
+
"type": "text",
|
|
30
|
+
"value": "Hello ",
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"hashtag": "hashtag",
|
|
34
|
+
"name": "hashtag",
|
|
35
|
+
"tag": [
|
|
36
|
+
"t",
|
|
37
|
+
"hashtag",
|
|
38
|
+
],
|
|
39
|
+
"type": "hashtag",
|
|
40
|
+
},
|
|
41
|
+
]
|
|
42
|
+
`);
|
|
43
|
+
});
|
|
44
|
+
it("should not match hashtags with missing t tags", () => {
|
|
45
|
+
expect(getParsedContent(user.event({ content: "Hello #hashtag", tags: [] }), undefined, [hashtags]).children).not.toEqual(expect.arrayContaining([expect.objectContaining({ type: "hashtag" })]));
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
});
|
package/dist/text/content.js
CHANGED
|
@@ -3,7 +3,7 @@ import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
|
|
|
3
3
|
import { nostrMentions } from "./mentions.js";
|
|
4
4
|
import { cashuTokens } from "./cashu.js";
|
|
5
5
|
import { emojis } from "./emoji.js";
|
|
6
|
-
import {
|
|
6
|
+
import { createEventContentTree } from "./parser.js";
|
|
7
7
|
import { hashtags } from "./hashtag.js";
|
|
8
8
|
import { galleries } from "./gallery.js";
|
|
9
9
|
import { lightningInvoices } from "./lightning.js";
|
|
@@ -29,7 +29,7 @@ export function getParsedContent(event, content, transformers = textNoteTransfor
|
|
|
29
29
|
for (const transformer of transformers) {
|
|
30
30
|
processor.use(transformer);
|
|
31
31
|
}
|
|
32
|
-
return processor.runSync(
|
|
32
|
+
return processor.runSync(createEventContentTree(event, content));
|
|
33
33
|
}
|
|
34
34
|
// no caching
|
|
35
35
|
if (!cacheKey) {
|
|
@@ -37,14 +37,14 @@ export function getParsedContent(event, content, transformers = textNoteTransfor
|
|
|
37
37
|
for (const transformer of transformers) {
|
|
38
38
|
processor.use(transformer);
|
|
39
39
|
}
|
|
40
|
-
return processor.runSync(
|
|
40
|
+
return processor.runSync(createEventContentTree(event, content));
|
|
41
41
|
}
|
|
42
42
|
return getOrComputeCachedValue(event, cacheKey, () => {
|
|
43
43
|
const processor = unified();
|
|
44
44
|
for (const transformer of transformers) {
|
|
45
45
|
processor.use(transformer);
|
|
46
46
|
}
|
|
47
|
-
return processor.runSync(
|
|
47
|
+
return processor.runSync(createEventContentTree(event, content));
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
export function removeParsedTextContent(event) {
|
package/dist/text/emoji.d.ts
CHANGED
package/dist/text/emoji.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getEmojiTag } from "applesauce-core/helpers/emoji";
|
|
2
2
|
import { Tokens } from "../helpers/regexp.js";
|
|
3
3
|
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
-
/**
|
|
4
|
+
/** Finds and creates emoji nodes in the tree */
|
|
5
5
|
export function emojis() {
|
|
6
6
|
return (tree) => {
|
|
7
7
|
const event = tree.event;
|
package/dist/text/gallery.d.ts
CHANGED
package/dist/text/gallery.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { convertToUrl, getURLFilename, IMAGE_EXT } from "applesauce-core/helpers/url";
|
|
2
|
-
/**
|
|
2
|
+
/** Group images into galleries in an ATS tree */
|
|
3
3
|
export function galleries(types = IMAGE_EXT) {
|
|
4
4
|
return (tree) => {
|
|
5
5
|
let links = [];
|
package/dist/text/hashtag.d.ts
CHANGED
package/dist/text/hashtag.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { getHashtagTag } from "applesauce-core/helpers/hashtag";
|
|
2
2
|
import { Tokens } from "../helpers/regexp.js";
|
|
3
3
|
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
/** Find and create hashtag notes in provided tree */
|
|
4
5
|
export function hashtags() {
|
|
5
6
|
return (tree) => {
|
|
6
7
|
const event = tree.event;
|
|
7
|
-
if (!event)
|
|
8
|
-
return;
|
|
9
8
|
findAndReplace(tree, [
|
|
10
9
|
[
|
|
11
10
|
Tokens.hashtag,
|
|
12
11
|
(_, $1) => {
|
|
13
12
|
try {
|
|
14
|
-
const tag = getHashtagTag(event, $1);
|
|
15
|
-
if
|
|
13
|
+
const tag = event && getHashtagTag(event, $1);
|
|
14
|
+
// Skip if the match if no tag was found in the event
|
|
15
|
+
if (!tag && event)
|
|
16
16
|
return false;
|
|
17
17
|
return {
|
|
18
18
|
type: "hashtag",
|
|
19
19
|
tag,
|
|
20
20
|
name: $1,
|
|
21
|
-
hashtag: tag[1].toLowerCase(),
|
|
21
|
+
hashtag: tag?.[1].toLowerCase() || $1.toLowerCase(),
|
|
22
22
|
};
|
|
23
23
|
}
|
|
24
24
|
catch (error) { }
|
package/dist/text/lightning.d.ts
CHANGED
package/dist/text/lightning.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { parseBolt11 } from "applesauce-core/helpers/bolt11";
|
|
2
2
|
import { Tokens } from "../helpers/regexp.js";
|
|
3
3
|
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
/** Finds and creates lightning invoice nodes in the tree */
|
|
4
5
|
export function lightningInvoices() {
|
|
5
6
|
return (tree) => {
|
|
6
7
|
findAndReplace(tree, [
|
package/dist/text/links.d.ts
CHANGED
package/dist/text/links.js
CHANGED
package/dist/text/mentions.d.ts
CHANGED
package/dist/text/mentions.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { decode } from "nostr-tools/nip19";
|
|
2
2
|
import { Tokens } from "../helpers/regexp.js";
|
|
3
3
|
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
/** Finds and creates NIP-19 nostr mentions in the tree */
|
|
4
5
|
export function nostrMentions() {
|
|
5
6
|
return (tree) => {
|
|
6
7
|
findAndReplace(tree, [
|
package/dist/text/parser.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
2
2
|
import { Root } from "../nast/types.js";
|
|
3
3
|
/** Creates a {@link Root} ATS node for a text note */
|
|
4
|
-
export declare function
|
|
4
|
+
export declare function createEventContentTree(event: NostrEvent | EventTemplate | string, content?: string): Root;
|
|
5
|
+
/** @deprecated use createEventContentTree instead */
|
|
6
|
+
export declare const createTextNoteATS: typeof createEventContentTree;
|
package/dist/text/parser.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Creates a {@link Root} ATS node for a text note */
|
|
2
|
-
export function
|
|
2
|
+
export function createEventContentTree(event, content) {
|
|
3
3
|
return {
|
|
4
4
|
type: "root",
|
|
5
5
|
event: typeof event !== "string" ? event : undefined,
|
|
@@ -11,3 +11,5 @@ export function createTextNoteATS(event, content) {
|
|
|
11
11
|
],
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
+
/** @deprecated use createEventContentTree instead */
|
|
15
|
+
export const createTextNoteATS = createEventContentTree;
|
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-20250606170247",
|
|
4
4
|
"description": "Unified plugins for processing event content",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@types/hast": "^3.0.4",
|
|
53
53
|
"@types/mdast": "^4.0.4",
|
|
54
54
|
"@types/unist": "^3.0.3",
|
|
55
|
-
"applesauce-core": "0.0.0-next-
|
|
55
|
+
"applesauce-core": "0.0.0-next-20250606170247",
|
|
56
56
|
"mdast-util-find-and-replace": "^3.0.2",
|
|
57
57
|
"nostr-tools": "^2.13",
|
|
58
58
|
"remark": "^15.0.1",
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
},
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"typescript": "^5.8.3",
|
|
65
|
+
"applesauce-signers": "0.0.0-next-20250606170247",
|
|
65
66
|
"vitest": "^3.1.1"
|
|
66
67
|
},
|
|
67
68
|
"funding": {
|