Add tags from content to notes
This commit is contained in:
+37
-4
@@ -1,6 +1,7 @@
|
||||
import type {JSONContent, PasteRuleMatch, InputRuleMatch} from "@tiptap/core"
|
||||
import {Editor} from "@tiptap/core"
|
||||
import {choice} from "@welshman/lib"
|
||||
import {Address} from "@welshman/util"
|
||||
|
||||
export const asInline = (extend: Record<string, any>) => ({
|
||||
inline: true,
|
||||
@@ -34,20 +35,52 @@ export const findNodes = (type: string, json: JSONContent) => {
|
||||
return results
|
||||
}
|
||||
|
||||
export const findMarks = (type: string, json: JSONContent) => {
|
||||
const results: JSONContent[] = []
|
||||
|
||||
for (const node of json.content || []) {
|
||||
for (const mark of node.marks || []) {
|
||||
if (mark.type === type) {
|
||||
results.push(mark)
|
||||
}
|
||||
}
|
||||
|
||||
for (const result of findMarks(type, node)) {
|
||||
results.push(result)
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
export const getEditorTags = (editor: Editor) => {
|
||||
const json = editor.getJSON()
|
||||
|
||||
const withAttrs = (f: any) => (attrs: any) => f(attrs as Record<string, any>)
|
||||
const topicTags = findMarks("tag", json).map(
|
||||
({attrs}: any) => ["t", attrs.tag.replace(/^#/, '').toLowerCase()],
|
||||
)
|
||||
|
||||
const naddrTags = findNodes("naddr", json).map(
|
||||
({kind, pubkey, identifier, relays}: any) => {
|
||||
const address = new Address(kind, pubkey, identifier).toString()
|
||||
|
||||
return ["q", address, choice(relays) || "", pubkey]
|
||||
},
|
||||
)
|
||||
|
||||
const neventTags = findNodes("nevent", json).map(
|
||||
({id, author, relays}: any) => ["q", id, choice(relays) || "", author || ""],
|
||||
)
|
||||
|
||||
const mentionTags = findNodes("nprofile", json).map(
|
||||
withAttrs(({pubkey, relays}: any) => ["p", pubkey, choice(relays), ""]),
|
||||
({pubkey, relays}: any) => ["p", pubkey, choice(relays) || "", ""],
|
||||
)
|
||||
|
||||
const imetaTags = findNodes("image", json).map(
|
||||
withAttrs(({src, sha256}: any) => ["imeta", `url ${src}`, `x ${sha256}`, `ox ${sha256}`]),
|
||||
({src, sha256}: any) => ["imeta", `url ${src}`, `x ${sha256}`, `ox ${sha256}`],
|
||||
)
|
||||
|
||||
return [...mentionTags, ...imetaTags]
|
||||
return [...topicTags, ...naddrTags, ...neventTags, ...mentionTags, ...imetaTags]
|
||||
}
|
||||
|
||||
export const addFile = (editor: Editor) => editor.chain().selectFiles().run()
|
||||
|
||||
Reference in New Issue
Block a user