Bring back typedoc
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import {describe, it, expect} from "vitest"
|
||||
import * as Content from "../src"
|
||||
import {ParsedLink, ParsedType, parse, reduceLinks, renderAsHtml, renderAsText} from "../src"
|
||||
import {npubEncode, noteEncode} from "nostr-tools/nip19"
|
||||
|
||||
describe("Content Parsing", () => {
|
||||
@@ -7,51 +7,55 @@ describe("Content Parsing", () => {
|
||||
const nevent = noteEncode("ff".repeat(32))
|
||||
describe("Basic Parsing", () => {
|
||||
it("should parse plain text", () => {
|
||||
const result = Content.parse({content: "Hello world"})
|
||||
const result = parse({content: "Hello world"})
|
||||
expect(result).toEqual([
|
||||
{type: Content.ParsedType.Text, value: "Hello world", raw: "Hello world"},
|
||||
{type: ParsedType.Text, value: "Hello world", raw: "Hello world"},
|
||||
])
|
||||
})
|
||||
|
||||
it("should parse newlines", () => {
|
||||
const result = Content.parse({content: "Hello\nworld"})
|
||||
const result = parse({content: "Hello\nworld"})
|
||||
expect(result).toEqual([
|
||||
{type: Content.ParsedType.Text, value: "Hello", raw: "Hello"},
|
||||
{type: Content.ParsedType.Newline, value: "\n", raw: "\n"},
|
||||
{type: Content.ParsedType.Text, value: "world", raw: "world"},
|
||||
{type: ParsedType.Text, value: "Hello", raw: "Hello"},
|
||||
{type: ParsedType.Newline, value: "\n", raw: "\n"},
|
||||
{type: ParsedType.Text, value: "world", raw: "world"},
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("Link Parsing", () => {
|
||||
it("should parse basic URLs", () => {
|
||||
const result = Content.parse({content: "Check https://example.com"})
|
||||
expect(result[1]).toMatchObject({
|
||||
type: Content.ParsedType.Link,
|
||||
const result = parse({content: "Check https://example.com"})
|
||||
const parsed = result[1] as ParsedLink
|
||||
|
||||
expect(parsed).toMatchObject({
|
||||
type: ParsedType.Link,
|
||||
value: {
|
||||
url: expect.any(URL),
|
||||
// isMedia: false,
|
||||
},
|
||||
})
|
||||
expect(result[1].value.url.toString()).toBe("https://example.com/")
|
||||
|
||||
expect(parsed.value.url.toString()).toBe("https://example.com/")
|
||||
})
|
||||
|
||||
it("should parse URLs without protocol", () => {
|
||||
const result = Content.parse({content: "Visit example.com"})
|
||||
expect(result[1]).toMatchObject({
|
||||
type: Content.ParsedType.Link,
|
||||
const result = parse({content: "Visit example.com"})
|
||||
const parsed = result[1] as ParsedLink
|
||||
|
||||
expect(parsed).toMatchObject({
|
||||
type: ParsedType.Link,
|
||||
value: {
|
||||
url: expect.any(URL),
|
||||
// isMedia: false,
|
||||
},
|
||||
})
|
||||
expect(result[1].value.url.toString()).toBe("https://example.com/")
|
||||
|
||||
expect(parsed.value.url.toString()).toBe("https://example.com/")
|
||||
})
|
||||
|
||||
it("should identify media links", () => {
|
||||
const result = Content.parse({content: "https://example.com/image.jpg"})
|
||||
const result = parse({content: "https://example.com/image.jpg"})
|
||||
expect(result[0]).toMatchObject({
|
||||
type: Content.ParsedType.Link,
|
||||
type: ParsedType.Link,
|
||||
value: {
|
||||
url: expect.any(URL),
|
||||
meta: {},
|
||||
@@ -62,46 +66,46 @@ describe("Content Parsing", () => {
|
||||
|
||||
describe("Nostr Entity Parsing", () => {
|
||||
it("should parse nostr profiles", () => {
|
||||
const result = Content.parse({
|
||||
const result = parse({
|
||||
content: `nostr:${npub}`,
|
||||
})
|
||||
|
||||
expect(result[0]).toMatchObject({
|
||||
type: Content.ParsedType.Profile,
|
||||
type: ParsedType.Profile,
|
||||
})
|
||||
})
|
||||
|
||||
it("should parse nostr events", () => {
|
||||
const result = Content.parse({
|
||||
const result = parse({
|
||||
content: `nostr:${nevent}`,
|
||||
})
|
||||
expect(result[0]).toMatchObject({
|
||||
type: Content.ParsedType.Event,
|
||||
type: ParsedType.Event,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("Special Content Parsing", () => {
|
||||
it("should parse code blocks", () => {
|
||||
const result = Content.parse({content: "```const x = 1```"})
|
||||
const result = parse({content: "```const x = 1```"})
|
||||
expect(result[0]).toMatchObject({
|
||||
type: Content.ParsedType.Code,
|
||||
type: ParsedType.Code,
|
||||
value: "const x = 1",
|
||||
})
|
||||
})
|
||||
|
||||
it("should parse inline code", () => {
|
||||
const result = Content.parse({content: "Use `npm install`"})
|
||||
const result = parse({content: "Use `npm install`"})
|
||||
expect(result[1]).toMatchObject({
|
||||
type: Content.ParsedType.Code,
|
||||
type: ParsedType.Code,
|
||||
value: "npm install",
|
||||
})
|
||||
})
|
||||
|
||||
it("should parse topics", () => {
|
||||
const result = Content.parse({content: "#nostr is cool"})
|
||||
const result = parse({content: "#nostr is cool"})
|
||||
expect(result[0]).toMatchObject({
|
||||
type: Content.ParsedType.Topic,
|
||||
type: ParsedType.Topic,
|
||||
value: "nostr",
|
||||
})
|
||||
})
|
||||
@@ -109,27 +113,27 @@ describe("Content Parsing", () => {
|
||||
|
||||
describe("Rendering", () => {
|
||||
it("should render as text", () => {
|
||||
const parsed = Content.parse({content: "Hello https://example.com"})
|
||||
const rendered = Content.renderAsText(parsed).toString()
|
||||
const parsed = parse({content: "Hello https://example.com"})
|
||||
const rendered = renderAsText(parsed).toString()
|
||||
expect(rendered).toContain("Hello")
|
||||
expect(rendered).toContain("https://example.com")
|
||||
})
|
||||
|
||||
it("should render as HTML", () => {
|
||||
const parsed = Content.parse({content: "Hello https://example.com"})
|
||||
const rendered = Content.renderAsHtml(parsed).toString()
|
||||
const parsed = parse({content: "Hello https://example.com"})
|
||||
const rendered = renderAsHtml(parsed).toString()
|
||||
expect(rendered).toContain('<a href="https://example.com/"')
|
||||
})
|
||||
})
|
||||
|
||||
describe("Link Grid", () => {
|
||||
it("should reduce consecutive image links into a grid", () => {
|
||||
const content = Content.parse({
|
||||
const content = parse({
|
||||
content: "https://example.com/1.jpg\nhttps://example.com/2.jpg https://example.com/2.jpg",
|
||||
})
|
||||
const reduced = Content.reduceLinks(content)
|
||||
const reduced = reduceLinks(content)
|
||||
expect(reduced[0]).toMatchObject({
|
||||
type: Content.ParsedType.LinkGrid,
|
||||
type: ParsedType.LinkGrid,
|
||||
value: {
|
||||
links: expect.any(Array),
|
||||
},
|
||||
@@ -139,12 +143,12 @@ describe("Content Parsing", () => {
|
||||
|
||||
describe("Legacy Mention Parsing", () => {
|
||||
it("should parse legacy mentions", () => {
|
||||
const result = Content.parse({
|
||||
const result = parse({
|
||||
content: "#[0]",
|
||||
tags: [["p", "1234567890"]],
|
||||
})
|
||||
expect(result[0]).toMatchObject({
|
||||
type: Content.ParsedType.Profile,
|
||||
type: ParsedType.Profile,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -36,7 +36,6 @@ describe("Content Truncation", () => {
|
||||
value: {
|
||||
url: new URL("https://example.com/image.jpg"),
|
||||
meta: {},
|
||||
isMedia: true,
|
||||
},
|
||||
raw: "https://example.com/image.jpg",
|
||||
},
|
||||
@@ -83,7 +82,6 @@ describe("Content Truncation", () => {
|
||||
value: {
|
||||
url: new URL("https://example.com/image.jpg"),
|
||||
meta: {},
|
||||
isMedia: true,
|
||||
},
|
||||
raw: "https://example.com/image.jpg",
|
||||
},
|
||||
@@ -141,8 +139,8 @@ describe("Content Truncation", () => {
|
||||
type: ParsedType.LinkGrid,
|
||||
value: {
|
||||
links: [
|
||||
{url: new URL("https://example.com/1.jpg"), meta: {}, isMedia: true},
|
||||
{url: new URL("https://example.com/2.jpg"), meta: {}, isMedia: true},
|
||||
{url: new URL("https://example.com/1.jpg"), meta: {}},
|
||||
{url: new URL("https://example.com/2.jpg"), meta: {}},
|
||||
],
|
||||
},
|
||||
raw: "",
|
||||
|
||||
Reference in New Issue
Block a user