lute.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. InlineSup: true,
  26. InlineSub: true,
  27. InlineTag: true,
  28. InlineMath: true,
  29. }
  30. type Markdown struct {
  31. InlineSup bool `json:"inlineSup"` // 是否启用行级上标
  32. InlineSub bool `json:"inlineSub"` // 是否启用行级下标
  33. InlineTag bool `json:"inlineTag"` // 是否启用行级标签
  34. InlineMath bool `json:"inlineMath"` // 是否启用行级公式
  35. }
  36. func NewLute() (ret *lute.Lute) {
  37. ret = lute.New()
  38. ret.SetTextMark(true)
  39. ret.SetProtyleWYSIWYG(true)
  40. ret.SetBlockRef(true)
  41. ret.SetFileAnnotationRef(true)
  42. ret.SetKramdownIAL(true)
  43. ret.SetTag(true)
  44. ret.SetSuperBlock(true)
  45. ret.SetImgPathAllowSpace(true)
  46. ret.SetGitConflict(true)
  47. ret.SetMark(true)
  48. ret.SetSup(MarkdownSettings.InlineSup)
  49. ret.SetSub(MarkdownSettings.InlineSub)
  50. ret.SetTag(MarkdownSettings.InlineTag)
  51. ret.SetInlineMath(MarkdownSettings.InlineMath)
  52. ret.SetInlineMathAllowDigitAfterOpenMarker(true)
  53. ret.SetGFMStrikethrough1(false)
  54. ret.SetFootnotes(false)
  55. ret.SetToC(false)
  56. ret.SetIndentCodeBlock(false)
  57. ret.SetParagraphBeginningSpace(true)
  58. ret.SetAutoSpace(false)
  59. ret.SetHeadingID(false)
  60. ret.SetSetext(false)
  61. ret.SetYamlFrontMatter(false)
  62. ret.SetLinkRef(false)
  63. ret.SetCodeSyntaxHighlight(false)
  64. ret.SetSanitize(true)
  65. return
  66. }
  67. func NewStdLute() (ret *lute.Lute) {
  68. ret = lute.New()
  69. ret.SetFootnotes(false)
  70. ret.SetToC(false)
  71. ret.SetIndentCodeBlock(false)
  72. ret.SetAutoSpace(false)
  73. ret.SetHeadingID(false)
  74. ret.SetSetext(false)
  75. ret.SetYamlFrontMatter(false)
  76. ret.SetLinkRef(false)
  77. ret.SetGFMAutoLink(false) // 导入 Markdown 时不自动转换超链接 https://github.com/siyuan-note/siyuan/issues/7682
  78. ret.SetImgPathAllowSpace(true)
  79. ret.SetInlineMathAllowDigitAfterOpenMarker(true) // Formula parsing supports $ followed by numbers when importing Markdown https://github.com/siyuan-note/siyuan/issues/8362
  80. ret.SetSup(MarkdownSettings.InlineSup)
  81. ret.SetSub(MarkdownSettings.InlineSub)
  82. ret.SetTag(MarkdownSettings.InlineTag)
  83. ret.SetInlineMath(MarkdownSettings.InlineMath)
  84. ret.SetGFMStrikethrough1(false)
  85. return
  86. }
  87. func LinkTarget(htmlStr, linkBase string) (ret string) {
  88. doc, err := goquery.NewDocumentFromReader(strings.NewReader(htmlStr))
  89. if nil != err {
  90. logging.LogErrorf("parse HTML failed: %s", err)
  91. return
  92. }
  93. doc.Find("a").Each(func(i int, selection *goquery.Selection) {
  94. if href, ok := selection.Attr("href"); ok {
  95. if IsRelativePath(href) {
  96. selection.SetAttr("href", linkBase+href)
  97. }
  98. // The hyperlink in the marketplace package README fails to jump to the browser to open https://github.com/siyuan-note/siyuan/issues/8452
  99. selection.SetAttr("target", "_blank")
  100. }
  101. })
  102. ret, _ = doc.Find("body").Html()
  103. return
  104. }