Skip to content

Commit

Permalink
feat: add strikethrough and underline
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Mar 3, 2020
1 parent 8642303 commit 23cef46
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 0 deletions.
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ Type: `string`

Text to be formatted italic

### strikethrough(text)

#### text

Type: `string`

Text to be striked through

### underline(text)

#### text

Type: `string`

Text to be underlined

### monospace(text)

#### text
Expand Down
10 changes: 10 additions & 0 deletions source/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ function italic(text: string): string {
return `<i>${escape(text)}</i>`
}

function strikethrough(text: string): string {
return `<s>${escape(text)}</s>`
}

function underline(text: string): string {
return `<u>${escape(text)}</u>`
}

function monospace(text: string): string {
return `<code>${escape(text)}</code>`
}
Expand All @@ -42,6 +50,8 @@ export const html: Formatter = {
escape,
bold,
italic,
strikethrough,
underline,
monospace,
monospaceBlock,
url,
Expand Down
2 changes: 2 additions & 0 deletions source/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface Formatter {
italic: (text: string) => string;
monospace: (text: string) => string;
monospaceBlock: (text: string, programmingLanguage?: string) => string;
strikethrough: (text: string) => string;
underline: (text: string) => string;
url: (label: string, url: string) => string;
userMention: (label: string, userId: number) => string;
}
10 changes: 10 additions & 0 deletions source/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ function italic(text: string): string {
return `_${text.replace(/_/g, '')}_`
}

function strikethrough(_text: string): string {
throw new Error('strikethrough is not supported by Markdown. Use MarkdownV2 instead.')
}

function underline(_text: string): string {
throw new Error('underline is not supported by Markdown. Use MarkdownV2 instead.')
}

function monospace(text: string): string {
return '`' + text.replace(/`/g, '') + '`'
}
Expand Down Expand Up @@ -46,6 +54,8 @@ export const markdown: Formatter = {
escape,
bold,
italic,
strikethrough,
underline,
monospace,
monospaceBlock,
url,
Expand Down
10 changes: 10 additions & 0 deletions source/markdownv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ function italic(text: string): string {
return `_${escape(text)}_`
}

function strikethrough(text: string): string {
return `~${escape(text)}~`
}

function underline(text: string): string {
return `__${escape(text)}__`
}

function monospace(text: string): string {
return '`' + escapeInteral(text, '`') + '`'
}
Expand Down Expand Up @@ -50,6 +58,8 @@ export const markdownv2: Formatter = {
escape,
bold,
italic,
strikethrough,
underline,
monospace,
monospaceBlock,
url,
Expand Down
8 changes: 8 additions & 0 deletions test/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ test('italic', t => {
t.is(format.italic('italic'), '<i>italic</i>')
})

test('strikethrough', t => {
t.is(format.strikethrough('strikethrough'), '<s>strikethrough</s>')
})

test('underline', t => {
t.is(format.underline('underline'), '<u>underline</u>')
})

test('url', t => {
t.is(format.url('me', 'https://edjopato.de'), '<a href="https://edjopato.de">me</a>')
})
Expand Down
18 changes: 18 additions & 0 deletions test/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,21 @@ test('monospaceBlock w/o language', t => {
test('monospaceBlock w/ language', t => {
t.is(format.monospaceBlock('pre-formatted fixed-width code block written in the Python programming language', 'python'), '```python\npre-formatted fixed-width code block written in the Python programming language\n```')
})

test('strikethrough', t => {
t.throws(
() => format.strikethrough('1337'),
{
message: 'strikethrough is not supported by Markdown. Use MarkdownV2 instead.'
}
)
})

test('underline', t => {
t.throws(
() => format.underline('1337'),
{
message: 'underline is not supported by Markdown. Use MarkdownV2 instead.'
}
)
})
8 changes: 8 additions & 0 deletions test/markdownv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ test('italic', t => {
t.is(format.italic('italic'), '_italic_')
})

test('strikethrough', t => {
t.is(format.strikethrough('strikethrough'), '~strikethrough~')
})

test('underline', t => {
t.is(format.underline('underline'), '__underline__')
})

test('url', t => {
t.is(format.url('me', 'https://edjopato.de'), '[me](https://edjopato.de)')
})
Expand Down

0 comments on commit 23cef46

Please sign in to comment.