Wrap inline code in single backticks

This commit is contained in:
Bohdan Horbeshko 2022-03-11 12:01:38 -05:00
parent f6e62fe1fd
commit cdbd960ff4
2 changed files with 30 additions and 6 deletions

View file

@ -20,7 +20,9 @@ type InsertionStack []*Insertion
var boldRunesMarkdown = []rune("**") var boldRunesMarkdown = []rune("**")
var boldRunesXEP0393 = []rune("*") var boldRunesXEP0393 = []rune("*")
var italicRunes = []rune("_") var italicRunes = []rune("_")
var codeRunes = []rune("\n```\n") var codeRunes = []rune("`")
var preRuneStart = []rune("```\n")
var preRuneEnd = []rune("\n```")
// rebalance pumps all the values until the given offset to current stack (growing // rebalance pumps all the values until the given offset to current stack (growing
// from start) from given stack (growing from end); should be called // from start) from given stack (growing from end); should be called
@ -131,8 +133,10 @@ func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown) return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown)
case client.TypeTextEntityTypeItalic: case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes) return markupBraces(entity, italicRunes, italicRunes)
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre: case client.TypeTextEntityTypeCode:
return markupBraces(entity, codeRunes, codeRunes) return markupBraces(entity, codeRunes, codeRunes)
case client.TypeTextEntityTypePre:
return markupBraces(entity, preRuneStart, preRuneEnd)
case client.TypeTextEntityTypePreCode: case client.TypeTextEntityTypePreCode:
preCode, _ := entity.Type.(*client.TextEntityTypePreCode) preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes) return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
@ -155,11 +159,13 @@ func EntityToXEP0393(entity *client.TextEntity) (*Insertion, *Insertion) {
return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393) return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393)
case client.TypeTextEntityTypeItalic: case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes) return markupBraces(entity, italicRunes, italicRunes)
case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre: case client.TypeTextEntityTypeCode:
// inline code is non-standard
return markupBraces(entity, codeRunes, codeRunes) return markupBraces(entity, codeRunes, codeRunes)
case client.TypeTextEntityTypePre:
return markupBraces(entity, preRuneStart, preRuneEnd)
case client.TypeTextEntityTypePreCode: case client.TypeTextEntityTypePreCode:
preCode, _ := entity.Type.(*client.TextEntityTypePreCode) preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
// TODO: inline code support (non-standard too)
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes) return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
case client.TypeTextEntityTypeTextUrl: case client.TypeTextEntityTypeTextUrl:
textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl) textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl)

View file

@ -64,7 +64,7 @@ func TestFormattingAdjacentAndNested(t *testing.T) {
Type: &client.TextEntityTypeItalic{}, Type: &client.TextEntityTypeItalic{},
}, },
}, EntityToMarkdown) }, EntityToMarkdown)
if markup != "\n```\n**👙**🐧\n```\n_🐖_" { if markup != "```\n**👙**🐧\n```_🐖_" {
t.Errorf("Wrong adjacent&nested formatting: %v", markup) t.Errorf("Wrong adjacent&nested formatting: %v", markup)
} }
} }
@ -265,7 +265,7 @@ func TestFormattingXEP0393AdjacentAndNested(t *testing.T) {
Type: &client.TextEntityTypeItalic{}, Type: &client.TextEntityTypeItalic{},
}, },
}, EntityToXEP0393) }, EntityToXEP0393)
if markup != "\n```\n*👙*🐧\n```\n_🐖_" { if markup != "```\n*👙*🐧\n```_🐖_" {
t.Errorf("Wrong adjacent&nested formatting: %v", markup) t.Errorf("Wrong adjacent&nested formatting: %v", markup)
} }
} }
@ -348,3 +348,21 @@ func TestFormattingXEP0393Intersecting(t *testing.T) {
t.Errorf("Wrong intersecting formatting: %v", markup) t.Errorf("Wrong intersecting formatting: %v", markup)
} }
} }
func TestFormattingXEP0393InlineCode(t *testing.T) {
markup := Format("Is Gajim a thing?\n\necho 'Hello'\necho 'world'\n\nhruck(", []*client.TextEntity{
&client.TextEntity{
Offset: 3,
Length: 5,
Type: &client.TextEntityTypeCode{},
},
&client.TextEntity{
Offset: 19,
Length: 25,
Type: &client.TextEntityTypePre{},
},
}, EntityToXEP0393)
if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
t.Errorf("Wrong intersecting formatting: %v", markup)
}
}