applesauce-content 0.0.0-next-20250123205825 → 0.0.0-next-20250123215242
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/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/regexp.d.ts +9 -0
- package/dist/helpers/regexp.js +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/markdown/index.d.ts +1 -0
- package/dist/markdown/index.js +1 -0
- package/dist/markdown/mentions.d.ts +8 -0
- package/dist/markdown/mentions.js +22 -0
- package/dist/nast/eol-metadata.d.ts +3 -0
- package/dist/nast/eol-metadata.js +14 -0
- package/dist/nast/find-and-replace.d.ts +6 -0
- package/dist/nast/find-and-replace.js +95 -0
- package/dist/nast/index.d.ts +4 -0
- package/dist/nast/index.js +4 -0
- package/dist/nast/truncate.d.ts +2 -0
- package/dist/nast/truncate.js +48 -0
- package/dist/nast/types.d.ts +71 -0
- package/dist/nast/types.js +1 -0
- package/dist/text/cashu.d.ts +4 -0
- package/dist/text/cashu.js +25 -0
- package/dist/text/content.d.ts +9 -0
- package/dist/text/content.js +53 -0
- package/dist/text/emoji.d.ts +4 -0
- package/dist/text/emoji.js +32 -0
- package/dist/text/gallery.d.ts +4 -0
- package/dist/text/gallery.js +48 -0
- package/dist/text/hashtag.d.ts +3 -0
- package/dist/text/hashtag.js +30 -0
- package/dist/text/index.d.ts +9 -0
- package/dist/text/index.js +9 -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/mentions.d.ts +3 -0
- package/dist/text/mentions.js +23 -0
- package/dist/text/parser.d.ts +4 -0
- package/dist/text/parser.js +13 -0
- package/package.json +2 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./regexp.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./regexp.js";
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
},
|
|
5
|
+
get cashu() {
|
|
6
|
+
return /(cashu(?:A|B)[A-Za-z0-9_-]{100,10000}={0,3})/gi;
|
|
7
|
+
},
|
|
8
|
+
get nostrLink() {
|
|
9
|
+
return /nostr:((npub|note|nprofile|nevent|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,})/gi;
|
|
10
|
+
},
|
|
11
|
+
get emoji() {
|
|
12
|
+
return /:([a-zA-Z0-9_-]+):/gi;
|
|
13
|
+
},
|
|
14
|
+
get hashtag() {
|
|
15
|
+
return /(?<=^|[^\p{L}#])#([\p{L}\p{N}\p{M}]+)/gu;
|
|
16
|
+
},
|
|
17
|
+
get lightning() {
|
|
18
|
+
return /(?:lightning:)?(LNBC[A-Za-z0-9]+)/gim;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
export default Expressions;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./mentions.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./mentions.js";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Transformer } from "unified";
|
|
2
|
+
import { Link, Nodes } from "mdast";
|
|
3
|
+
import { DecodeResult } from "nostr-tools/nip19";
|
|
4
|
+
export interface NostrMention extends Link {
|
|
5
|
+
type: "link";
|
|
6
|
+
data: DecodeResult;
|
|
7
|
+
}
|
|
8
|
+
export declare function remarkNostrMentions(): Transformer<Nodes>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { findAndReplace } from "mdast-util-find-and-replace";
|
|
2
|
+
import { decode } from "nostr-tools/nip19";
|
|
3
|
+
import Expressions from "../helpers/regexp.js";
|
|
4
|
+
export function remarkNostrMentions() {
|
|
5
|
+
return (tree) => {
|
|
6
|
+
findAndReplace(tree, [
|
|
7
|
+
Expressions.nostrLink,
|
|
8
|
+
(_, $1) => {
|
|
9
|
+
try {
|
|
10
|
+
return {
|
|
11
|
+
type: "link",
|
|
12
|
+
data: decode($1),
|
|
13
|
+
children: [],
|
|
14
|
+
url: "nostr:" + $1,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
catch (error) { }
|
|
18
|
+
return false;
|
|
19
|
+
},
|
|
20
|
+
]);
|
|
21
|
+
};
|
|
22
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function eolMetadata() {
|
|
2
|
+
return (tree) => {
|
|
3
|
+
for (let i = 0; i < tree.children.length; i++) {
|
|
4
|
+
const node = tree.children[i];
|
|
5
|
+
const next = tree.children[i + 1];
|
|
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;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Content, Root } from "./types.js";
|
|
2
|
+
type Replace = (...groups: string[]) => null | undefined | false | string | Content | Content[];
|
|
3
|
+
type FindAndReplaceTuple = [RegExp, Replace];
|
|
4
|
+
type FindAndReplaceList = FindAndReplaceTuple[];
|
|
5
|
+
export declare function findAndReplace(tree: Root, list: FindAndReplaceList): void;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { visitParents } from "unist-util-visit-parents";
|
|
2
|
+
export function findAndReplace(tree, list) {
|
|
3
|
+
const pairs = list;
|
|
4
|
+
let pairIndex = -1;
|
|
5
|
+
const visitor = (node, parents) => {
|
|
6
|
+
let index = -1;
|
|
7
|
+
/** @type {Parents | undefined} */
|
|
8
|
+
let grandparent;
|
|
9
|
+
while (++index < parents.length) {
|
|
10
|
+
const parent = parents[index];
|
|
11
|
+
// const siblings = grandparent ? grandparent.children : undefined;
|
|
12
|
+
grandparent = parent;
|
|
13
|
+
}
|
|
14
|
+
if (grandparent) {
|
|
15
|
+
return handler(node, parents);
|
|
16
|
+
}
|
|
17
|
+
return undefined;
|
|
18
|
+
};
|
|
19
|
+
while (++pairIndex < pairs.length) {
|
|
20
|
+
visitParents(tree, "text", visitor);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Handle a text node which is not in an ignored parent.
|
|
24
|
+
*
|
|
25
|
+
* @param {Text} node
|
|
26
|
+
* Text node.
|
|
27
|
+
* @param {Array<Parents>} parents
|
|
28
|
+
* Parents.
|
|
29
|
+
* @returns {VisitorResult}
|
|
30
|
+
* Result.
|
|
31
|
+
*/
|
|
32
|
+
function handler(node, parents) {
|
|
33
|
+
const parent = parents[parents.length - 1];
|
|
34
|
+
const find = pairs[pairIndex][0];
|
|
35
|
+
const replace = pairs[pairIndex][1];
|
|
36
|
+
let start = 0;
|
|
37
|
+
const siblings = parent.children;
|
|
38
|
+
const index = siblings.indexOf(node);
|
|
39
|
+
let change = false;
|
|
40
|
+
let nodes = [];
|
|
41
|
+
find.lastIndex = 0;
|
|
42
|
+
let match = find.exec(node.value);
|
|
43
|
+
while (match) {
|
|
44
|
+
const position = match.index;
|
|
45
|
+
/** @type {RegExpMatchObject} */
|
|
46
|
+
// const matchObject = {
|
|
47
|
+
// index: match.index,
|
|
48
|
+
// input: match.input,
|
|
49
|
+
// stack: [...parents, node],
|
|
50
|
+
// };
|
|
51
|
+
// let value = replace(...match, matchObject);
|
|
52
|
+
let value = replace(...match);
|
|
53
|
+
if (typeof value === "string") {
|
|
54
|
+
value = value.length > 0 ? { type: "text", value } : undefined;
|
|
55
|
+
}
|
|
56
|
+
// It wasn’t a match after all.
|
|
57
|
+
if (value === false) {
|
|
58
|
+
// False acts as if there was no match.
|
|
59
|
+
// So we need to reset `lastIndex`, which currently being at the end of
|
|
60
|
+
// the current match, to the beginning.
|
|
61
|
+
find.lastIndex = position + 1;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
if (start !== position) {
|
|
65
|
+
nodes.push({
|
|
66
|
+
type: "text",
|
|
67
|
+
value: node.value.slice(start, position),
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
if (Array.isArray(value)) {
|
|
71
|
+
nodes.push(...value);
|
|
72
|
+
}
|
|
73
|
+
else if (value) {
|
|
74
|
+
nodes.push(value);
|
|
75
|
+
}
|
|
76
|
+
start = position + match[0].length;
|
|
77
|
+
change = true;
|
|
78
|
+
}
|
|
79
|
+
if (!find.global) {
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
match = find.exec(node.value);
|
|
83
|
+
}
|
|
84
|
+
if (change) {
|
|
85
|
+
if (start < node.value.length) {
|
|
86
|
+
nodes.push({ type: "text", value: node.value.slice(start) });
|
|
87
|
+
}
|
|
88
|
+
parent.children.splice(index, 1, ...nodes);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
nodes = [node];
|
|
92
|
+
}
|
|
93
|
+
return index + nodes.length;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export function truncateContent(tree, maxLength = 256) {
|
|
2
|
+
let length = 0;
|
|
3
|
+
for (let i = 0; i < tree.children.length; i++) {
|
|
4
|
+
const node = tree.children[i];
|
|
5
|
+
switch (node.type) {
|
|
6
|
+
case "hashtag":
|
|
7
|
+
length += 1 + node.hashtag.length;
|
|
8
|
+
break;
|
|
9
|
+
case "mention":
|
|
10
|
+
// guess user names are about 10 long
|
|
11
|
+
length += 10;
|
|
12
|
+
break;
|
|
13
|
+
case "cashu":
|
|
14
|
+
length += node.raw.length;
|
|
15
|
+
break;
|
|
16
|
+
case "gallery":
|
|
17
|
+
length += node.links.reduce((t, l) => t + l.length, 0);
|
|
18
|
+
break;
|
|
19
|
+
case "link":
|
|
20
|
+
case "text":
|
|
21
|
+
length += node.value.length;
|
|
22
|
+
break;
|
|
23
|
+
case "emoji":
|
|
24
|
+
length += 1;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
if (length > maxLength) {
|
|
28
|
+
if (node.type === "text") {
|
|
29
|
+
const children = i > 0 ? tree.children.slice(0, i) : [];
|
|
30
|
+
const chunkLength = node.value.length - (length - maxLength);
|
|
31
|
+
// find the nearest newline
|
|
32
|
+
const newLines = node.value.matchAll(/\n/g);
|
|
33
|
+
for (const match of newLines) {
|
|
34
|
+
if (match.index && match.index > chunkLength) {
|
|
35
|
+
children.push({ type: "text", value: node.value.slice(0, match.index) });
|
|
36
|
+
return { ...tree, children, truncated: true };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// just cut the string
|
|
40
|
+
children.push({ type: "text", value: node.value.slice(0, maxLength - length) });
|
|
41
|
+
return { ...tree, children, truncated: true };
|
|
42
|
+
}
|
|
43
|
+
else
|
|
44
|
+
return { ...tree, children: tree.children.slice(0, i), truncated: true };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return tree;
|
|
48
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
2
|
+
import { DecodeResult } from "nostr-tools/nip19";
|
|
3
|
+
import { Node as UnistNode, Parent } from "unist";
|
|
4
|
+
import { type Token } from "@cashu/cashu-ts";
|
|
5
|
+
import { type ParsedInvoice } from "applesauce-core/helpers/bolt11";
|
|
6
|
+
export interface CommonData {
|
|
7
|
+
eol?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface Node extends Omit<UnistNode, "data"> {
|
|
10
|
+
data?: CommonData;
|
|
11
|
+
}
|
|
12
|
+
export interface Text extends Node {
|
|
13
|
+
type: "text";
|
|
14
|
+
value: string;
|
|
15
|
+
}
|
|
16
|
+
export interface Link extends Node {
|
|
17
|
+
type: "link";
|
|
18
|
+
value: string;
|
|
19
|
+
href: string;
|
|
20
|
+
}
|
|
21
|
+
export interface Gallery extends Node {
|
|
22
|
+
type: "gallery";
|
|
23
|
+
links: string[];
|
|
24
|
+
}
|
|
25
|
+
export interface Mention extends Node {
|
|
26
|
+
type: "mention";
|
|
27
|
+
decoded: DecodeResult;
|
|
28
|
+
encoded: string;
|
|
29
|
+
}
|
|
30
|
+
export interface CashuToken extends Node {
|
|
31
|
+
type: "cashu";
|
|
32
|
+
token: Token;
|
|
33
|
+
raw: string;
|
|
34
|
+
}
|
|
35
|
+
export interface LightningInvoice extends Node {
|
|
36
|
+
type: "lightning";
|
|
37
|
+
invoice: string;
|
|
38
|
+
parsed: ParsedInvoice;
|
|
39
|
+
}
|
|
40
|
+
export interface Hashtag extends Node {
|
|
41
|
+
type: "hashtag";
|
|
42
|
+
/** The name as it was written in the event */
|
|
43
|
+
name: string;
|
|
44
|
+
/** The lowercase canonical name */
|
|
45
|
+
hashtag: string;
|
|
46
|
+
tag: ["t", ...string[]];
|
|
47
|
+
}
|
|
48
|
+
export interface Emoji extends Node {
|
|
49
|
+
type: "emoji";
|
|
50
|
+
code: string;
|
|
51
|
+
raw: string;
|
|
52
|
+
url: string;
|
|
53
|
+
tag: ["emoji", ...string[]];
|
|
54
|
+
}
|
|
55
|
+
export interface ContentMap {
|
|
56
|
+
text: Text;
|
|
57
|
+
link: Link;
|
|
58
|
+
mention: Mention;
|
|
59
|
+
cashu: CashuToken;
|
|
60
|
+
lightning: LightningInvoice;
|
|
61
|
+
hashtag: Hashtag;
|
|
62
|
+
emoji: Emoji;
|
|
63
|
+
gallery: Gallery;
|
|
64
|
+
}
|
|
65
|
+
export type Content = ContentMap[keyof ContentMap];
|
|
66
|
+
export interface Root extends Parent {
|
|
67
|
+
type: "root";
|
|
68
|
+
children: Content[];
|
|
69
|
+
event?: NostrEvent | EventTemplate;
|
|
70
|
+
truncated?: boolean;
|
|
71
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getDecodedToken } from "@cashu/cashu-ts";
|
|
2
|
+
import Expressions from "../helpers/regexp.js";
|
|
3
|
+
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
/** Parse cashu tokens from an ATS tree */
|
|
5
|
+
export function cashuTokens() {
|
|
6
|
+
return (tree) => {
|
|
7
|
+
findAndReplace(tree, [
|
|
8
|
+
[
|
|
9
|
+
Expressions.cashu,
|
|
10
|
+
(_, $1) => {
|
|
11
|
+
try {
|
|
12
|
+
const token = getDecodedToken($1);
|
|
13
|
+
return {
|
|
14
|
+
type: "cashu",
|
|
15
|
+
token,
|
|
16
|
+
raw: $1,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
catch (error) { }
|
|
20
|
+
return false;
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
]);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Transformer } from "unified";
|
|
2
|
+
import { EventTemplate, NostrEvent } from "nostr-tools";
|
|
3
|
+
import { Root } from "../nast/types.js";
|
|
4
|
+
import { galleries } from "./gallery.js";
|
|
5
|
+
export declare const TextNoteContentSymbol: unique symbol;
|
|
6
|
+
export declare const textNoteTransformers: (typeof galleries)[];
|
|
7
|
+
/** Parsed and process a note with custom transformers */
|
|
8
|
+
export declare function getParsedContent(event: NostrEvent | EventTemplate | string, content?: string, transformers?: (() => Transformer<Root>)[], cacheKey?: symbol | null | undefined): Root;
|
|
9
|
+
export declare function removeParsedTextContent(event: NostrEvent | EventTemplate): void;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { unified } from "unified";
|
|
2
|
+
import { getOrComputeCachedValue } from "applesauce-core/helpers/cache";
|
|
3
|
+
import { nostrMentions } from "./mentions.js";
|
|
4
|
+
import { cashuTokens } from "./cashu.js";
|
|
5
|
+
import { emojis } from "./emoji.js";
|
|
6
|
+
import { createTextNoteATS } from "./parser.js";
|
|
7
|
+
import { hashtags } from "./hashtag.js";
|
|
8
|
+
import { galleries } from "./gallery.js";
|
|
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");
|
|
13
|
+
// default kind 1 transformers
|
|
14
|
+
export const textNoteTransformers = [
|
|
15
|
+
links,
|
|
16
|
+
nostrMentions,
|
|
17
|
+
galleries,
|
|
18
|
+
emojis,
|
|
19
|
+
hashtags,
|
|
20
|
+
lightningInvoices,
|
|
21
|
+
cashuTokens,
|
|
22
|
+
eolMetadata,
|
|
23
|
+
];
|
|
24
|
+
/** Parsed and process a note with custom transformers */
|
|
25
|
+
export function getParsedContent(event, content, transformers = textNoteTransformers, cacheKey = TextNoteContentSymbol) {
|
|
26
|
+
// process strings
|
|
27
|
+
if (typeof event === "string") {
|
|
28
|
+
const processor = unified();
|
|
29
|
+
for (const transformer of transformers) {
|
|
30
|
+
processor.use(transformer);
|
|
31
|
+
}
|
|
32
|
+
return processor.runSync(createTextNoteATS(event, content));
|
|
33
|
+
}
|
|
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, () => {
|
|
43
|
+
const processor = unified();
|
|
44
|
+
for (const transformer of transformers) {
|
|
45
|
+
processor.use(transformer);
|
|
46
|
+
}
|
|
47
|
+
return processor.runSync(createTextNoteATS(event, content));
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
export function removeParsedTextContent(event) {
|
|
51
|
+
// @ts-expect-error
|
|
52
|
+
delete event[TextNoteContentSymbol];
|
|
53
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { getEmojiTag } from "applesauce-core/helpers/emoji";
|
|
2
|
+
import Expressions from "../helpers/regexp.js";
|
|
3
|
+
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
/** Adds emoji tags to text ATS */
|
|
5
|
+
export function emojis() {
|
|
6
|
+
return (tree) => {
|
|
7
|
+
const event = tree.event;
|
|
8
|
+
if (!event)
|
|
9
|
+
return;
|
|
10
|
+
findAndReplace(tree, [
|
|
11
|
+
[
|
|
12
|
+
Expressions.emoji,
|
|
13
|
+
(full, $1) => {
|
|
14
|
+
try {
|
|
15
|
+
const tag = getEmojiTag(event, $1);
|
|
16
|
+
if (!tag)
|
|
17
|
+
return false;
|
|
18
|
+
return {
|
|
19
|
+
type: "emoji",
|
|
20
|
+
tag,
|
|
21
|
+
raw: full,
|
|
22
|
+
code: tag[1].toLowerCase(),
|
|
23
|
+
url: tag[2],
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
catch (error) { }
|
|
27
|
+
return false;
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
]);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { convertToUrl, getURLFilename, IMAGE_EXT } from "applesauce-core/helpers/url";
|
|
2
|
+
/** Groups images into galleries in an ATS tree */
|
|
3
|
+
export function galleries(types = IMAGE_EXT) {
|
|
4
|
+
return (tree) => {
|
|
5
|
+
let links = [];
|
|
6
|
+
const commit = (index) => {
|
|
7
|
+
// only create a gallery if there are more than a single image
|
|
8
|
+
if (links.length > 1) {
|
|
9
|
+
const start = tree.children.indexOf(links[0]);
|
|
10
|
+
const end = tree.children.indexOf(links[links.length - 1]);
|
|
11
|
+
// replace all nodes with a gallery
|
|
12
|
+
tree.children.splice(start, 1 + end - start, { type: "gallery", links: links.map((l) => l.href) });
|
|
13
|
+
links = [];
|
|
14
|
+
// return new cursor
|
|
15
|
+
return end - 1;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
links = [];
|
|
19
|
+
return index;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
for (let i = 0; i < tree.children.length; i++) {
|
|
23
|
+
const node = tree.children[i];
|
|
24
|
+
try {
|
|
25
|
+
if (node.type === "link") {
|
|
26
|
+
const url = convertToUrl(node.href);
|
|
27
|
+
const filename = getURLFilename(url);
|
|
28
|
+
if (filename && types.some((ext) => filename.endsWith(ext))) {
|
|
29
|
+
links.push(node);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
i = commit(i);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
else if (node.type === "text" && links.length > 0) {
|
|
36
|
+
const isEmpty = node.value === "\n" || !node.value.match(/[^\s]/g);
|
|
37
|
+
if (!isEmpty)
|
|
38
|
+
i = commit(i);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
i = commit(i);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Do one finally commit, just in case a link is the last element in the list
|
|
46
|
+
commit(tree.children.length);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getHashtagTag } from "applesauce-core/helpers/hashtag";
|
|
2
|
+
import Expressions from "../helpers/regexp.js";
|
|
3
|
+
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
export function hashtags() {
|
|
5
|
+
return (tree) => {
|
|
6
|
+
const event = tree.event;
|
|
7
|
+
if (!event)
|
|
8
|
+
return;
|
|
9
|
+
findAndReplace(tree, [
|
|
10
|
+
[
|
|
11
|
+
Expressions.hashtag,
|
|
12
|
+
(_, $1) => {
|
|
13
|
+
try {
|
|
14
|
+
const tag = getHashtagTag(event, $1);
|
|
15
|
+
if (!tag)
|
|
16
|
+
return false;
|
|
17
|
+
return {
|
|
18
|
+
type: "hashtag",
|
|
19
|
+
tag,
|
|
20
|
+
name: $1,
|
|
21
|
+
hashtag: tag[1].toLowerCase(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
catch (error) { }
|
|
25
|
+
return false;
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
]);
|
|
29
|
+
};
|
|
30
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./content.js";
|
|
2
|
+
export * from "./links.js";
|
|
3
|
+
export * from "./mentions.js";
|
|
4
|
+
export * from "./cashu.js";
|
|
5
|
+
export * from "./emoji.js";
|
|
6
|
+
export * from "./parser.js";
|
|
7
|
+
export * from "./hashtag.js";
|
|
8
|
+
export * from "./gallery.js";
|
|
9
|
+
export * from "./lightning.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from "./content.js";
|
|
2
|
+
export * from "./links.js";
|
|
3
|
+
export * from "./mentions.js";
|
|
4
|
+
export * from "./cashu.js";
|
|
5
|
+
export * from "./emoji.js";
|
|
6
|
+
export * from "./parser.js";
|
|
7
|
+
export * from "./hashtag.js";
|
|
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
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { decode } from "nostr-tools/nip19";
|
|
2
|
+
import Expressions from "../helpers/regexp.js";
|
|
3
|
+
import { findAndReplace } from "../nast/find-and-replace.js";
|
|
4
|
+
export function nostrMentions() {
|
|
5
|
+
return (tree) => {
|
|
6
|
+
findAndReplace(tree, [
|
|
7
|
+
[
|
|
8
|
+
Expressions.nostrLink,
|
|
9
|
+
(_, $1) => {
|
|
10
|
+
try {
|
|
11
|
+
return {
|
|
12
|
+
type: "mention",
|
|
13
|
+
decoded: decode($1),
|
|
14
|
+
encoded: $1,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
catch (error) { }
|
|
18
|
+
return false;
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
]);
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Creates a {@link Root} ATS node for a text note */
|
|
2
|
+
export function createTextNoteATS(event, content) {
|
|
3
|
+
return {
|
|
4
|
+
type: "root",
|
|
5
|
+
event: typeof event !== "string" ? event : undefined,
|
|
6
|
+
children: [
|
|
7
|
+
{
|
|
8
|
+
type: "text",
|
|
9
|
+
value: content || (typeof event === "string" ? event : event.content),
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
};
|
|
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-20250123215242",
|
|
4
4
|
"description": "Unified plugins for processing event content",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"@types/hast": "^3.0.4",
|
|
46
46
|
"@types/mdast": "^4.0.4",
|
|
47
47
|
"@types/unist": "^3.0.3",
|
|
48
|
-
"applesauce-core": "0.0.0-next-
|
|
48
|
+
"applesauce-core": "0.0.0-next-20250123215242",
|
|
49
49
|
"mdast-util-find-and-replace": "^3.0.1",
|
|
50
50
|
"nostr-tools": "^2.10.3",
|
|
51
51
|
"remark": "^15.0.1",
|