Skip to content

Commit

Permalink
feat(campaign): add campaign editor and normalizer; rename journal to…
Browse files Browse the repository at this point in the history
… moment;
  • Loading branch information
robertu7 committed Jul 8, 2024
1 parent 3a458f0 commit d2236be
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@matters/matters-editor",
"version": "0.2.5-alpha.8",
"version": "0.2.5-alpha.9",
"description": "Editor for matters.news",
"author": "https://github.com/thematters",
"homepage": "https://github.com/thematters/matters-editor",
Expand Down
29 changes: 29 additions & 0 deletions src/editors/Campaign.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { type EditorOptions, useEditor } from '@tiptap/react'

import {
makeCampaignEditorExtensions,
type MakeCampaignEditorExtensionsProps,
} from './extensions'

type UseCampaignEditorProps = {
content: string
} & MakeCampaignEditorExtensionsProps &
Partial<EditorOptions>

export const useCampaignEditor = ({
content,
placeholder,
...editorProps
}: UseCampaignEditorProps) => {
const { extensions, ...restProps } = editorProps
const editor = useEditor({
extensions: [
...makeCampaignEditorExtensions({ placeholder }),
...(extensions ?? []),
],
content,
...restProps,
})

return editor
}
14 changes: 7 additions & 7 deletions src/editors/Journal.tsx → src/editors/Moment.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { type EditorOptions, useEditor } from '@tiptap/react'

import {
makeJournalEditorExtensions,
type MakeJournalEditorExtensionsProps,
makeMomentEditorExtensions,
type MakeMomentEditorExtensionsProps,
} from './extensions'

type UseJournalEditorProps = {
type UseMomentEditorProps = {
content: string
} & MakeJournalEditorExtensionsProps &
} & MakeMomentEditorExtensionsProps &
Partial<EditorOptions>

export const useJournalEditor = ({
export const useMomentEditor = ({
content,
placeholder,
mentionSuggestion,
...editorProps
}: UseJournalEditorProps) => {
}: UseMomentEditorProps) => {
const { extensions, ...restProps } = editorProps
const editor = useEditor({
extensions: [
...makeJournalEditorExtensions({ placeholder, mentionSuggestion }),
...makeMomentEditorExtensions({ placeholder, mentionSuggestion }),
...(extensions ?? []),
],
content,
Expand Down
37 changes: 33 additions & 4 deletions src/editors/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,17 @@ export const makeCommentEditorExtensions = ({
}

/**
* Journal
* Moment
*/
export interface MakeJournalEditorExtensionsProps {
export interface MakeMomentEditorExtensionsProps {
placeholder?: string
mentionSuggestion?: MentionSuggestion
}

export const makeJournalEditorExtensions = ({
export const makeMomentEditorExtensions = ({
placeholder,
mentionSuggestion,
}: MakeJournalEditorExtensionsProps) => {
}: MakeMomentEditorExtensionsProps) => {
const extensions = [...baseExtensions(placeholder)]

if (mentionSuggestion) {
Expand All @@ -130,3 +130,32 @@ export const makeJournalEditorExtensions = ({

return extensions
}

/**
* Campaign
*/
export interface MakeCampaignEditorExtensionsProps {
placeholder?: string
}

export const makeCampaignEditorExtensions = ({
placeholder,
}: MakeCampaignEditorExtensionsProps) => {
return [
Document,
History,
Placeholder.configure({
placeholder,
}),
// Basic Formats
Text,
Paragraph,
HardBreak.configure({
HTMLAttributes: {
class: 'smart',
},
}),
// Custom Formats
Link,
]
}
3 changes: 2 additions & 1 deletion src/editors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './Article'
export * from './Campaign'
export * from './Comment'
export * from './Journal'
export * from './Moment'
export * from '@tiptap/react'
50 changes: 50 additions & 0 deletions src/transformers/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from 'vitest'

import {
normalizeArticleHTML,
normalizeCampaignHTML,
normalizeCommentHTML,
NormalizeOptions,
} from './normalize'
Expand All @@ -25,6 +26,15 @@ const expectNormalizeCommentHTML = (
expect(result.trim()).toBe(output)
}

const expectNormalizeCampaignHTML = (
input: string,
output: string,
options?: NormalizeOptions,
) => {
const result = normalizeCampaignHTML(input, options)
expect(result.trim()).toBe(output)
}

/**
* Tests
*/
Expand Down Expand Up @@ -439,3 +449,43 @@ describe('Normalization: Comment', () => {
)
})
})

describe('Normalization: Campaign', () => {
test('quote is not supported', () => {
expectNormalizeCampaignHTML(
stripIndent`
<blockquote>
<p>1</p>
<p>2</p>
<p>3</p>
</blockquote>
`,
'<p>1</p><p>2</p><p>3</p>',
)
})

test('bold is not supported', () => {
expectNormalizeCampaignHTML('<p><strong>abc</strong></p>', '<p>abc</p>')
expectNormalizeCampaignHTML('<p><b>abc</b></p>', '<p>abc</p>')
})

test('strikethrough is not supported', () => {
expectNormalizeCampaignHTML('<p><s>abc</s></p>', '<p>abc</p>')
expectNormalizeCampaignHTML('<p><del>abc</del></p>', '<p>abc</p>')
expectNormalizeCampaignHTML('<p><strike>abc</strike></p>', '<p>abc</p>')
})

test('link is supported', () => {
expectNormalizeCampaignHTML(
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="https://example.com">abc</a></p>',
'<p><a target="_blank" rel="noopener noreferrer nofollow" href="https://example.com">abc</a></p>',
)
})

test('line break', () => {
expectNormalizeCampaignHTML(
'<p>hello,<br>world</p>',
'<p>hello,<br class="smart">world</p>',
)
})
})
23 changes: 20 additions & 3 deletions src/transformers/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { createHTMLDocument, parseHTML, type VHTMLDocument } from 'zeed-dom'

import {
makeArticleEditorExtensions,
makeCampaignEditorExtensions,
makeCommentEditorExtensions,
makeJournalEditorExtensions,
makeMomentEditorExtensions,
Mention,
} from '../editors/extensions'

Expand Down Expand Up @@ -100,11 +101,11 @@ export const normalizeCommentHTML = (
return normalizedHtml
}

export const normalizeJournalHTML = (
export const normalizeMomentHTML = (
html: string,
options?: NormalizeOptions,
): string => {
const extensions = makeJournalEditorExtensions({})
const extensions = makeMomentEditorExtensions({})
const normalizer = makeNormalizer([...extensions, Mention])

let normalizedHtml = normalizer(html)
Expand All @@ -115,3 +116,19 @@ export const normalizeJournalHTML = (

return normalizedHtml
}

export const normalizeCampaignHTML = (
html: string,
options?: NormalizeOptions,
): string => {
const extensions = makeCampaignEditorExtensions({})
const normalizer = makeNormalizer(extensions)

let normalizedHtml = normalizer(html)

if (options?.truncate) {
normalizedHtml = truncateLinkText(normalizedHtml, options.truncate)
}

return normalizedHtml
}

0 comments on commit d2236be

Please sign in to comment.