Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,10 @@ class Markup {
}

static formatHTML (text = '', entities = []) {
const chars = [...text]
const available = [...entities]
const opened = []
const result = []
for (let offset = 0; offset < chars.length; offset++) {
for (let offset = 0; offset < text.length; offset++) {
while (true) {
const index = available.findIndex((entity) => entity.offset === offset)
if (index === -1) {
Expand Down Expand Up @@ -213,7 +212,7 @@ class Markup {
available.splice(index, 1)
}

result.push(chars[offset])
result.push(escapeHTML(text[offset]))

while (true) {
const index = opened.findIndex((entity) => entity.offset + entity.length - 1 === offset)
Expand Down Expand Up @@ -256,6 +255,18 @@ class Markup {
}
}

const escapedChars = {
'"': '&quot;',
'&': '&amp;',
'<': '&lt;',
'>': '&gt;'
}

function escapeHTML (string) {
const chars = [...string]
return chars.map(char => escapedChars[char] || char).join('')
}

function buildKeyboard (buttons, options) {
const result = []
if (!Array.isArray(buttons)) {
Expand Down
34 changes: 33 additions & 1 deletion test/markup.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ test('should generate nested multi markup', (t) => {
t.deepEqual(markup, '<s>strike<b>bold<u>under</u></b></s>')
})

test.only('should generate nested multi markup 2', (t) => {
test('should generate nested multi markup 2', (t) => {
const markup = Markup.formatHTML('×11 22 333× ×С123456× ×1 22 333×', [
{
offset: 1,
Expand Down Expand Up @@ -252,3 +252,35 @@ test.only('should generate nested multi markup 2', (t) => {
])
t.deepEqual(markup, '×<b><i>11 22 333</i></b>× <i> ×С</i><i><b>123456× </b> ×1 22 333×</i>')
})

test('should generate correct markup with emojis', (t) => {
const markup = Markup.formatHTML('bold🙂👨‍👩‍👧‍👧 italic', [
{
offset: 0,
length: 6,
type: 'bold'
},
{
offset: 18,
length: 6,
type: 'italic'
}
])
t.deepEqual(markup, '<b>bold🙂</b>👨‍👩‍👧‍👧 <i>italic</i>')
})

test('should generate correct markup with HTML tags', (t) => {
const markup = Markup.formatHTML('<b>bold</b> <i>italic</i>', [
{
offset: 3,
length: 4,
type: 'bold'
},
{
offset: 15,
length: 6,
type: 'italic'
}
])
t.deepEqual(markup, '&lt;b&gt;<b>bold</b>&lt;/b&gt; &lt;i&gt;<i>italic</i>&lt;/i&gt;')
})