Skip to content

Commit

Permalink
feat: add monospace
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Jan 7, 2020
1 parent 782c957 commit 465f08a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
22 changes: 22 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ Type: `string`

Text to be formatted italic

### monospace(text)

#### text

Type: `string`

Text to be displayed as inline monospace

### monospaceBlock(text, [programmingLanguage])

#### text

Type: `string`

Text to be displayed as monospace block

#### programmingLanguage

Type: `string` (optional)

Can be used to specify the programming language of the code

### url(label, url)

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

function monospace(text: string): string {
return `<code>${text}</code>`
}

function monospaceBlock(text: string, programmingLanguage?: string): string {
if (programmingLanguage) {
return `<pre><code class="language-${programmingLanguage}">${text}</code></pre>`
}

return `<pre>${text}</pre>`
}

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

function monospace(text: string): string {
return '`' + text + '`'
}

function monospaceBlock(text: string, programmingLanguage?: string): string {
let result = ''
result += '```'

if (programmingLanguage) {
result += programmingLanguage
}

result += '\n'
result += text
result += '\n'
result += '```'
return result
}

function url(label: string, url: string): string {
return `[${label.replace(/\]/, '')}](${url.replace(/\)/, '')})`
}
Expand All @@ -27,6 +46,8 @@ export const markdown: Formatter = {
escape,
bold,
italic,
monospace,
monospaceBlock,
url,
userMention
}
12 changes: 12 additions & 0 deletions test/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,15 @@ test('bold malicious', t => {
test('user mention', t => {
t.is(format.userMention('inline mention of a user', 123456789), '<a href="tg://user?id=123456789">inline mention of a user</a>')
})

test('monospace', t => {
t.is(format.monospace('inline fixed-width code'), '<code>inline fixed-width code</code>')
})

test('monospaceBlock w/o language', t => {
t.is(format.monospaceBlock('pre-formatted fixed-width code block'), '<pre>pre-formatted fixed-width code block</pre>')
})

test('monospaceBlock w/ language', t => {
t.is(format.monospaceBlock('pre-formatted fixed-width code block written in the Python programming language', 'python'), '<pre><code class="language-python">pre-formatted fixed-width code block written in the Python programming language</code></pre>')
})
12 changes: 12 additions & 0 deletions test/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,15 @@ test('italic malicious', t => {
test('user mention', t => {
t.is(format.userMention('inline mention of a user', 123456789), '[inline mention of a user](tg://user?id=123456789)')
})

test('monospace', t => {
t.is(format.monospace('inline fixed-width code'), '`inline fixed-width code`')
})

test('monospaceBlock w/o language', t => {
t.is(format.monospaceBlock('pre-formatted fixed-width code block'), '```\npre-formatted fixed-width code block\n```')
})

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```')
})

0 comments on commit 465f08a

Please sign in to comment.