applesauce-content 0.7.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/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/helpers/regexp.d.ts +7 -0
- package/dist/helpers/regexp.js +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -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 +12 -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 +69 -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 +33 -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 +46 -0
- package/dist/text/hashtag.d.ts +3 -0
- package/dist/text/hashtag.js +30 -0
- package/dist/text/index.d.ts +6 -0
- package/dist/text/index.js +6 -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 +15 -0
- package/package.json +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 hzrd149
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const Expressions = {
|
|
2
|
+
get cashu() {
|
|
3
|
+
return /(cashu(?:A|B)[A-Za-z0-9_-]{100,10000}={0,3})/gi;
|
|
4
|
+
},
|
|
5
|
+
get nostrLink() {
|
|
6
|
+
return /(?:nostr:)?((npub|note|nprofile|nevent|nrelay|naddr)1[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{58,})/gi;
|
|
7
|
+
},
|
|
8
|
+
get emoji() {
|
|
9
|
+
return /:([a-zA-Z0-9_-]+):/gi;
|
|
10
|
+
},
|
|
11
|
+
get hashtag() {
|
|
12
|
+
return /(?:^|[^\p{L}])#([\p{L}\p{N}\p{M}]+)/gu;
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
export default Expressions;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./text/index.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./text/index.js";
|
|
@@ -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,12 @@
|
|
|
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 (next && next.type === "text" && next.value.startsWith("\n")) {
|
|
7
|
+
next.data = next.data || {};
|
|
8
|
+
next.data.eol = true;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
}
|
|
@@ -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,69 @@
|
|
|
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
|
+
export interface CommonData {
|
|
6
|
+
eol?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface Node extends Omit<UnistNode, "data"> {
|
|
9
|
+
data?: CommonData;
|
|
10
|
+
}
|
|
11
|
+
export interface Text extends Node {
|
|
12
|
+
type: "text";
|
|
13
|
+
value: string;
|
|
14
|
+
}
|
|
15
|
+
export interface Link extends Node {
|
|
16
|
+
type: "link";
|
|
17
|
+
value: string;
|
|
18
|
+
href: string;
|
|
19
|
+
}
|
|
20
|
+
export interface Gallery extends Node {
|
|
21
|
+
type: "gallery";
|
|
22
|
+
links: string[];
|
|
23
|
+
}
|
|
24
|
+
export interface Mention extends Node {
|
|
25
|
+
type: "mention";
|
|
26
|
+
decoded: DecodeResult;
|
|
27
|
+
encoded: string;
|
|
28
|
+
}
|
|
29
|
+
export interface CashuToken extends Node {
|
|
30
|
+
type: "cashu";
|
|
31
|
+
token: Token;
|
|
32
|
+
raw: string;
|
|
33
|
+
}
|
|
34
|
+
export interface LightningInvoice extends Node {
|
|
35
|
+
type: "lightning";
|
|
36
|
+
invoice: string;
|
|
37
|
+
}
|
|
38
|
+
export interface Hashtag extends Node {
|
|
39
|
+
type: "hashtag";
|
|
40
|
+
/** The name as it was written in the event */
|
|
41
|
+
name: string;
|
|
42
|
+
/** The lowercase canonical name */
|
|
43
|
+
hashtag: string;
|
|
44
|
+
tag: ["t", ...string[]];
|
|
45
|
+
}
|
|
46
|
+
export interface Emoji extends Node {
|
|
47
|
+
type: "emoji";
|
|
48
|
+
code: string;
|
|
49
|
+
raw: string;
|
|
50
|
+
url: string;
|
|
51
|
+
tag: ["emoji", ...string[]];
|
|
52
|
+
}
|
|
53
|
+
export interface ContentMap {
|
|
54
|
+
text: Text;
|
|
55
|
+
link: Link;
|
|
56
|
+
mention: Mention;
|
|
57
|
+
cashu: CashuToken;
|
|
58
|
+
lightning: LightningInvoice;
|
|
59
|
+
hashtag: Hashtag;
|
|
60
|
+
emoji: Emoji;
|
|
61
|
+
gallery: Gallery;
|
|
62
|
+
}
|
|
63
|
+
export type Content = ContentMap[keyof ContentMap];
|
|
64
|
+
export interface Root extends Parent {
|
|
65
|
+
type: "root";
|
|
66
|
+
children: Content[];
|
|
67
|
+
event?: NostrEvent | EventTemplate;
|
|
68
|
+
truncated?: boolean;
|
|
69
|
+
}
|
|
@@ -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 ParsedTextContentSymbol: unique symbol;
|
|
6
|
+
export declare const defaultTransformers: (typeof galleries)[];
|
|
7
|
+
/** Parsed and process a note with custom transformers */
|
|
8
|
+
export declare function getParsedTextContent(event: NostrEvent | EventTemplate | string, content?: string, transformers?: (() => Transformer<Root>)[]): Root;
|
|
9
|
+
export declare function removeParsedTextContent(event: NostrEvent | EventTemplate): void;
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
export const ParsedTextContentSymbol = Symbol.for("parsed-text-content");
|
|
10
|
+
// default kind 1 transformers
|
|
11
|
+
export const defaultTransformers = [nostrMentions, galleries, emojis, hashtags, cashuTokens];
|
|
12
|
+
/** Parsed and process a note with custom transformers */
|
|
13
|
+
export function getParsedTextContent(event, content, transformers = defaultTransformers) {
|
|
14
|
+
// process strings
|
|
15
|
+
if (typeof event === "string") {
|
|
16
|
+
const processor = unified();
|
|
17
|
+
for (const transformer of transformers) {
|
|
18
|
+
processor.use(transformer);
|
|
19
|
+
}
|
|
20
|
+
return processor.runSync(createTextNoteATS(event, content));
|
|
21
|
+
}
|
|
22
|
+
return getOrComputeCachedValue(event, ParsedTextContentSymbol, () => {
|
|
23
|
+
const processor = unified();
|
|
24
|
+
for (const transformer of transformers) {
|
|
25
|
+
processor.use(transformer);
|
|
26
|
+
}
|
|
27
|
+
return processor.runSync(createTextNoteATS(event, content));
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
export function removeParsedTextContent(event) {
|
|
31
|
+
// @ts-expect-error
|
|
32
|
+
delete event[ParsedTextContentSymbol];
|
|
33
|
+
}
|
|
@@ -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,46 @@
|
|
|
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 = (i) => {
|
|
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 i;
|
|
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
|
+
};
|
|
46
|
+
}
|
|
@@ -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,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,15 @@
|
|
|
1
|
+
import { tokenize } from "linkifyjs";
|
|
2
|
+
/** Creates a {@link Root} ATS node for a text note */
|
|
3
|
+
export function createTextNoteATS(event, content) {
|
|
4
|
+
const tokens = tokenize(content || (typeof event === "string" ? event : event.content));
|
|
5
|
+
return {
|
|
6
|
+
type: "root",
|
|
7
|
+
event: typeof event !== "string" ? event : undefined,
|
|
8
|
+
children: tokens.map((token) => {
|
|
9
|
+
if (token.isLink)
|
|
10
|
+
return { type: "link", href: token.toHref(), value: token.toString() };
|
|
11
|
+
else
|
|
12
|
+
return { type: "text", value: token.v };
|
|
13
|
+
}),
|
|
14
|
+
};
|
|
15
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "applesauce-content",
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "Unified plugins for processing event content",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"nostr"
|
|
10
|
+
],
|
|
11
|
+
"author": "hzrd149",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"import": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts"
|
|
20
|
+
},
|
|
21
|
+
"./helpers": {
|
|
22
|
+
"import": "./dist/helpers/index.js",
|
|
23
|
+
"types": "./dist/helpers/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./nast": {
|
|
26
|
+
"import": "./dist/nast/index.js",
|
|
27
|
+
"types": "./dist/nast/index.d.ts"
|
|
28
|
+
},
|
|
29
|
+
"./markdown": {
|
|
30
|
+
"import": "./dist/markdown/index.js",
|
|
31
|
+
"types": "./dist/markdown/index.d.ts"
|
|
32
|
+
},
|
|
33
|
+
"./text": {
|
|
34
|
+
"import": "./dist/text/index.js",
|
|
35
|
+
"types": "./dist/text/index.d.ts"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@cashu/cashu-ts": "^1.1.0",
|
|
40
|
+
"@types/hast": "^3.0.4",
|
|
41
|
+
"@types/mdast": "^4.0.4",
|
|
42
|
+
"@types/unist": "^3.0.3",
|
|
43
|
+
"applesauce-core": "^0.7.0",
|
|
44
|
+
"linkifyjs": "^4.1.3",
|
|
45
|
+
"mdast-util-find-and-replace": "^3.0.1",
|
|
46
|
+
"nostr-tools": "^2.7.2",
|
|
47
|
+
"remark": "^15.0.1",
|
|
48
|
+
"remark-parse": "^11.0.0",
|
|
49
|
+
"unified": "^11.0.5",
|
|
50
|
+
"unist-util-visit-parents": "^6.0.1"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@jest/globals": "^29.7.0",
|
|
54
|
+
"@types/jest": "^29.5.13",
|
|
55
|
+
"jest": "^29.7.0",
|
|
56
|
+
"jest-extended": "^4.0.2"
|
|
57
|
+
},
|
|
58
|
+
"jest": {
|
|
59
|
+
"roots": [
|
|
60
|
+
"dist"
|
|
61
|
+
],
|
|
62
|
+
"setupFilesAfterEnv": [
|
|
63
|
+
"jest-extended/all"
|
|
64
|
+
]
|
|
65
|
+
},
|
|
66
|
+
"scripts": {
|
|
67
|
+
"build": "tsc",
|
|
68
|
+
"watch:build": "tsc --watch > /dev/null",
|
|
69
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --passWithNoTests",
|
|
70
|
+
"watch:test": "(trap 'kill 0' SIGINT; pnpm run build -w > /dev/null & pnpm run test --watch)"
|
|
71
|
+
}
|
|
72
|
+
}
|