渲染markdown为html并录入数据库
(id int)
| 154 | |
| 155 | // 渲染markdown为html并录入数据库 |
| 156 | func RenderDocumentById(id int) { |
| 157 | //使用chromium-browser |
| 158 | // chromium-browser --headless --disable-gpu --screenshot --no-sandbox --window-size=320,480 http://www.bookstack.cn |
| 159 | link := "http://localhost:" + beego.AppConfig.DefaultString("httpport", "8181") + "/local-render?id=" + strconv.Itoa(id) |
| 160 | chrome := beego.AppConfig.DefaultString("chrome", "chromium-browser") |
| 161 | chromeArgs := []string{chrome, "--headless", "--disable-gpu", "--screenshot", "--no-sandbox", "--window-size=320,480", link} |
| 162 | nodeArgs := []string{"node", "crawl.js", "--url", link} |
| 163 | for _, v := range [][]string{chromeArgs, nodeArgs} { |
| 164 | cmd := exec.Command(v[0], v[1:]...) |
| 165 | beego.Info("render document by document_id:", cmd.Args) |
| 166 | // 超过10秒,杀掉进程,避免长期占用 |
| 167 | time.AfterFunc(httpTimeout, func() { |
| 168 | if cmd.Process != nil && cmd.Process.Pid != 0 { |
| 169 | cmd.Process.Kill() |
| 170 | } |
| 171 | }) |
| 172 | if err := cmd.Run(); err != nil { |
| 173 | beego.Error(err) |
| 174 | continue |
| 175 | } |
| 176 | return |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | const screenshotCoverJS = `'use strict'; |
| 181 | // 说明: 这段js是用来进行书籍封面截屏的,请勿随便删除或改动 |
no test coverage detected