Skip to content
This repository has been archived by the owner on Sep 17, 2023. It is now read-only.

Commit

Permalink
✨feat(#8): 添加粗糙的反链功能
Browse files Browse the repository at this point in the history
  • Loading branch information
2234839 committed Apr 8, 2021
1 parent 132cbc1 commit 4ba0e48
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ go run .\src\ "C:/Users/llej/AppData/Local/Programs/SiYuan/resources/guide/思
|| 块链接可 copy | |
|| 书签页 | |
|| 标签页 | |
| | 反链 | |
| ✅🔨 | [#8 反链](https://github.com/siyuan-note/oceanpress/issues/8) | `50%` |
| ✅🔨 | [#1 块引用链接](https://github.com/siyuan-note/oceanpress/issues/1) | `92%` |
|| 支持 {.text} 这样的锚文本 | `100%` |
|| [#3 代码高亮 以及 数学公式和脑图等渲染](https://github.com/siyuan-note/oceanpress/issues/3) [点击这里查看生成后的效果](https://siyuan-note.github.io/oceanpress/Markdown%20%e4%bd%bf%e7%94%a8%e6%8c%87%e5%8d%97/3%20%e5%ae%8c%e6%95%b4%e7%a4%ba%e4%be%8b.html) 、还需要修改 vditor 等资源的引用为本地文件(不是很重要之后再说) | `100%` |
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/2234839/md2website
go 1.15

require (
github.com/88250/lute v1.7.1
github.com/88250/lute v1.7.1 // https://pkg.go.dev/github.com/88250/lute
github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2 // indirect
github.com/jinzhu/gorm v1.9.16 // indirect
github.com/mattn/go-sqlite3 v1.14.6
Expand Down
41 changes: 32 additions & 9 deletions src/store/toHTML.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Generate(db sqlite.DbResult, FindFileEntityFromID FindFileEntityFromID, str
luteEngine.Md2HTMLRendererFuncs[ast.NodeBlockRefID] = getBlockID
luteEngine.Md2HTMLRendererFuncs[ast.NodeBlockEmbedID] = getBlockID

// HOC 内部处理了循环引用的问题, 生成一个渲染函数,
// HOC, 内部处理了循环引用的问题, 生成一个渲染函数,
GeneterateRenderFunction := func(render func(n *ast.Node, entering bool, src string, fileEntity FileEntity, mdInfo MdStructInfo, html string) string) func(n *ast.Node, entering bool) (string, ast.WalkStatus) {
return func(n *ast.Node, entering bool) (string, ast.WalkStatus) {
var html string
Expand Down Expand Up @@ -141,13 +141,11 @@ func Generate(db sqlite.DbResult, FindFileEntityFromID FindFileEntityFromID, str
})
})

/** 嵌入块查询渲染, 这个东西也应该包含在一个前端组件中 */
luteEngine.Md2HTMLRendererFuncs[ast.NodeBlockQueryEmbedScript] = GeneterateRenderFunction(func(n *ast.Node, entering bool, src string, fileEntity FileEntity, mdInfo MdStructInfo, html string) string {
sql := n.TokensStr()
// SqlRender 通过 sql 渲染出 html , curID 是当前块的 id
SqlRender := func(sql string, curID string, headerIncludes bool) string {
ids := db.SQLToID(sql)

curID := getNodeRelativeBlockID(n)

var html string
for _, id := range ids {
if id == curID {
// 排除当前块,显示自身并不方便阅读
Expand All @@ -171,20 +169,45 @@ func Generate(db sqlite.DbResult, FindFileEntityFromID FindFileEntityFromID, str
html += structToHTML(EmbeddedBlockInfo{
Src: src,
Title: src,
Content: template.HTML(luteEngine.MarkdownStr("", renderNodeMarkdown(mdInfo.node, true))),
Content: template.HTML(luteEngine.MarkdownStr("", renderNodeMarkdown(mdInfo.node, headerIncludes))),
})
luteEngine.RenderOptions.LinkBase = ""
pop(mdInfo.blockID)
}

}
return html
}
/** 嵌入块查询渲染, 这个东西也应该包含在一个前端组件中 */
luteEngine.Md2HTMLRendererFuncs[ast.NodeBlockQueryEmbedScript] = GeneterateRenderFunction(func(n *ast.Node, entering bool, src string, fileEntity FileEntity, mdInfo MdStructInfo, html string) string {
sql := n.TokensStr()
curID := getNodeRelativeBlockID(n)
return SqlRender(sql, curID, true)
})

// FileEntityToHTML 转 html
// FileEntityToHTML entity 转 html
FileEntityToHTML := func(entity FileEntity) string {
baseEntity = entity
return luteEngine.MarkdownStr("", entity.MdStr)

// 在每个文档的底部显示反链
curID := entity.MdStructInfoList[0].blockID

var refHTML string
content := SqlRender(`SELECT "refs".block_id as "ref_id", blocks.* FROM "refs"
LEFT JOIN blocks
ON "refs".block_id = blocks.id
WHERE
def_block_id = /** 被引用块的 id */ '`+curID+`'
;
`, curID, false)
if len(content) > 0 {
// 这里也应该使用模板,容后再做
refHTML = `<h2>链接到此文档的相关文档</h2>` + content
}

return luteEngine.MarkdownStr("", entity.MdStr) + refHTML
}
return FileEntityToHTML
}
Expand Down

0 comments on commit 4ba0e48

Please sign in to comment.