link_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. "github.com/usememos/memos/plugin/gomark/parser/tokenizer"
  6. )
  7. func TestLinkParser(t *testing.T) {
  8. tests := []struct {
  9. text string
  10. link *LinkParser
  11. }{
  12. {
  13. text: "[](https://example.com)",
  14. link: &LinkParser{
  15. ContentTokens: []*tokenizer.Token{
  16. {
  17. Type: tokenizer.Text,
  18. Value: "https://example.com",
  19. },
  20. },
  21. URL: "https://example.com",
  22. },
  23. },
  24. {
  25. text: "! [](https://example.com)",
  26. link: nil,
  27. },
  28. {
  29. text: "[alte]( htt ps :/ /example.com)",
  30. link: nil,
  31. },
  32. {
  33. text: "[hello world](https://example.com)",
  34. link: &LinkParser{
  35. ContentTokens: []*tokenizer.Token{
  36. {
  37. Type: tokenizer.Text,
  38. Value: "hello",
  39. },
  40. {
  41. Type: tokenizer.Space,
  42. Value: " ",
  43. },
  44. {
  45. Type: tokenizer.Text,
  46. Value: "world",
  47. },
  48. },
  49. URL: "https://example.com",
  50. },
  51. },
  52. }
  53. for _, test := range tests {
  54. tokens := tokenizer.Tokenize(test.text)
  55. require.Equal(t, test.link, NewLinkParser().Match(tokens))
  56. }
  57. }