// Vikunja is a to-do list application to facilitate your life. // Copyright 2018-present Vikunja and contributors. All rights reserved. // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . package richtext import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestHTMLToMarkdown(t *testing.T) { tests := []struct { name string html string want string }{ { name: "heading", html: "

Title

", want: "# Title", }, { name: "bold and italic", html: "

bold and italic

", want: "**bold** and *italic*", }, { name: "link", html: `

See the site

`, want: "See [the site](https://vikunja.io)", }, { name: "inline code", html: "

run mage build first

", want: "run `mage build` first", }, { name: "fenced code block keeps language", html: `
fmt.Println("hi")
`, want: "```go\nfmt.Println(\"hi\")\n```", }, { name: "blockquote", html: "

quoted text

", want: "> quoted text", }, { name: "unordered list", html: "", want: "- one\n- two", }, { name: "ordered list", html: "
  1. one
  2. two
", want: "1. one\n2. two", }, { name: "nested list", html: "", want: "- one\n \n - nested\n- two", }, { name: "gfm table", html: "
ab
12
", want: "| a | b |\n|---|---|\n| 1 | 2 |", }, { name: "strikethrough", html: "

gone

", want: "~~gone~~", }, { name: "empty paragraph is empty string", html: "

", want: "", }, { name: "whitespace only is empty string", html: "

", want: "", }, { name: "unknown element degrades without leaking tags", html: "

hello world

", want: "hello world", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got, err := HTMLToMarkdown(tt.html) require.NoError(t, err) assert.Equal(t, tt.want, got) }) } }