lute.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SiYuan - Refactor your thinking
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package util
  17. import (
  18. "strings"
  19. "github.com/88250/lute"
  20. "github.com/PuerkitoBio/goquery"
  21. "github.com/siyuan-note/logging"
  22. )
  23. // MarkdownSettings 运行时 Markdown 配置。
  24. var MarkdownSettings = &Markdown{
  25. InlineAsterisk: true,
  26. InlineUnderscore: true,
  27. InlineSup: true,
  28. InlineSub: true,
  29. InlineTag: true,
  30. InlineMath: true,
  31. InlineStrikethrough: true,
  32. }
  33. type Markdown struct {
  34. InlineAsterisk bool `json:"inlineAsterisk"` // 是否启用行级 * 语法
  35. InlineUnderscore bool `json:"inlineUnderscore"` // 是否启用行级 _ 语法
  36. InlineSup bool `json:"inlineSup"` // 是否启用行级上标
  37. InlineSub bool `json:"inlineSub"` // 是否启用行级下标
  38. InlineTag bool `json:"inlineTag"` // 是否启用行级标签
  39. InlineMath bool `json:"inlineMath"` // 是否启用行级公式
  40. InlineStrikethrough bool `json:"inlineStrikethrough"` // 是否启用行级删除线
  41. }
  42. func NewLute() (ret *lute.Lute) {
  43. ret = lute.New()
  44. ret.SetTextMark(true)
  45. ret.SetProtyleWYSIWYG(true)
  46. ret.SetBlockRef(true)
  47. ret.SetFileAnnotationRef(true)
  48. ret.SetKramdownIAL(true)
  49. ret.SetTag(true)
  50. ret.SetSuperBlock(true)
  51. ret.SetImgPathAllowSpace(true)
  52. ret.SetGitConflict(true)
  53. ret.SetMark(true)
  54. ret.SetInlineAsterisk(MarkdownSettings.InlineAsterisk)
  55. ret.SetInlineUnderscore(MarkdownSettings.InlineUnderscore)
  56. ret.SetSup(MarkdownSettings.InlineSup)
  57. ret.SetSub(MarkdownSettings.InlineSub)
  58. ret.SetTag(MarkdownSettings.InlineTag)
  59. ret.SetInlineMath(MarkdownSettings.InlineMath)
  60. ret.SetGFMStrikethrough(MarkdownSettings.InlineStrikethrough)
  61. ret.SetInlineMathAllowDigitAfterOpenMarker(true)
  62. ret.SetGFMStrikethrough1(false)
  63. ret.SetFootnotes(false)
  64. ret.SetToC(false)
  65. ret.SetIndentCodeBlock(false)
  66. ret.SetParagraphBeginningSpace(true)
  67. ret.SetAutoSpace(false)
  68. ret.SetHeadingID(false)
  69. ret.SetSetext(false)
  70. ret.SetYamlFrontMatter(false)
  71. ret.SetLinkRef(false)
  72. ret.SetCodeSyntaxHighlight(false)
  73. ret.SetSanitize(true)
  74. return
  75. }
  76. func NewStdLute() (ret *lute.Lute) {
  77. ret = lute.New()
  78. ret.SetFootnotes(false)
  79. ret.SetToC(false)
  80. ret.SetIndentCodeBlock(false)
  81. ret.SetAutoSpace(false)
  82. ret.SetHeadingID(false)
  83. ret.SetSetext(false)
  84. ret.SetYamlFrontMatter(false)
  85. ret.SetLinkRef(false)
  86. ret.SetGFMAutoLink(false) // 导入 Markdown 时不自动转换超链接 https://github.com/siyuan-note/siyuan/issues/7682
  87. ret.SetImgPathAllowSpace(true)
  88. ret.SetInlineMathAllowDigitAfterOpenMarker(true) // Formula parsing supports $ followed by numbers when importing Markdown https://github.com/siyuan-note/siyuan/issues/8362
  89. ret.SetInlineAsterisk(true)
  90. ret.SetInlineUnderscore(true)
  91. ret.SetSup(true)
  92. ret.SetSub(true)
  93. ret.SetTag(true)
  94. ret.SetInlineMath(true)
  95. ret.SetGFMStrikethrough(true)
  96. ret.SetGFMStrikethrough1(false)
  97. return
  98. }
  99. func LinkTarget(htmlStr, linkBase string) (ret string) {
  100. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr))
  101. if err != nil {
  102. logging.LogErrorf("parse HTML failed: %s", err)
  103. return
  104. }
  105. doc.Find("a").Each(func(i int, selection *goquery.Selection) {
  106. if href, ok := selection.Attr("href"); ok {
  107. if IsRelativePath(href) {
  108. selection.SetAttr("href", linkBase+href)
  109. }
  110. // The hyperlink in the marketplace package README fails to jump to the browser to open https://github.com/siyuan-note/siyuan/issues/8452
  111. selection.SetAttr("target", "_blank")
  112. }
  113. })
  114. ret, _ = doc.Find("body").Html()
  115. return
  116. }