@@ -238,7 +238,7 @@ func (this *BaseController) loginByMemberId(memberId int) (err error) {
238238}
239239
240240//在markdown头部加上<bookstack></bookstack>或者<bookstack/>,即解析markdown中的ul>li>a链接作为目录
241- func (this * BaseController ) sortBySummary (htmlstr string , book_id int ) {
241+ func (this * BaseController ) sortBySummary (bookIdentify , htmlstr string , book_id int ) {
242242 debug := beego .AppConfig .String ("runmod" ) != "prod"
243243 o := orm .NewOrm ()
244244 qs := o .QueryTable ("md_documents" ).Filter ("book_id" , book_id )
@@ -292,17 +292,18 @@ func (this *BaseController) sortBySummary(htmlstr string, book_id int) {
292292 })
293293 }
294294 }
295+
295296 }
296297
297298 doc .Find ("a" ).Each (func (i int , selection * goquery.Selection ) {
298299 doc_name := selection .Text ()
299- if docid , exist := selection .Attr ("data-bookstack" ); exist {
300+ pid := 0
301+ if docid , exist := selection .Attr ("data-pid" ); exist {
300302 doc_id , _ := strconv .Atoi (docid )
301303 eleParent := selection .Parent ().Parent ().Parent ()
302- pid := 0
303304 if eleParent .Is ("li" ) {
304305 fst := eleParent .Find ("a" ).First ()
305- pidstr , _ := fst .Attr ("data-bookstack " )
306+ pidstr , _ := fst .Attr ("data-pid " )
306307 //如果这里的pid为0,表示数据库还没存在这个标识,需要创建
307308 pid , _ = strconv .Atoi (pidstr )
308309 }
@@ -314,7 +315,6 @@ func (this *BaseController) sortBySummary(htmlstr string, book_id int) {
314315 }
315316 } else if href , ok := selection .Attr ("href" ); ok && strings .HasPrefix (href , "$" ) {
316317 identify := strings .TrimPrefix (href , "$" ) //文档标识
317- pid := 0 //默认上一级id,即pid
318318 eleParent := selection .Parent ().Parent ().Parent ()
319319 if eleParent .Is ("li" ) {
320320 if parentHref , ok := eleParent .Find ("a" ).First ().Attr ("href" ); ok {
@@ -333,6 +333,9 @@ func (this *BaseController) sortBySummary(htmlstr string, book_id int) {
333333 }
334334 idx ++
335335 })
336+ if len (hrefs ) > 0 { //如果有新创建的文档,则再调用一遍,用于处理排序
337+ this .replaceLinks (bookIdentify , htmlstr , true )
338+ }
336339}
337340
338341//排序
@@ -375,13 +378,13 @@ func (this *BaseController) replaceLinks(book_identify string, doc_html string,
375378 if newHref , ok := Links [strings .ToLower (slice [0 ])]; ok {
376379 arr := strings .Split (newHref , "||" ) //整理的arr数组长度,肯定为2,所以不做数组长度判断
377380 selection .SetAttr ("href" , arr [0 ]+ "#" + strings .Join (slice [1 :], "#" ))
378- selection .SetAttr ("data-bookstack " , arr [1 ])
381+ selection .SetAttr ("data-pid " , arr [1 ])
379382 }
380383 } else {
381384 if newHref , ok := Links [strings .ToLower (href )]; ok {
382385 arr := strings .Split (newHref , "||" ) //整理的arr数组长度,肯定为2,所以不做数组长度判断
383386 selection .SetAttr ("href" , arr [0 ])
384- selection .SetAttr ("data-bookstack " , arr [1 ])
387+ selection .SetAttr ("data-pid " , arr [1 ])
385388 }
386389 }
387390 }
@@ -390,7 +393,7 @@ func (this *BaseController) replaceLinks(book_identify string, doc_html string,
390393 if newHtml , err := gq .Find ("body" ).Html (); err == nil {
391394 doc_html = newHtml
392395 if len (is_summary ) > 0 && is_summary [0 ] == true { //更新排序
393- this .sortBySummary (doc_html , book .BookId ) //更新排序
396+ this .sortBySummary (book_identify , doc_html , book .BookId ) //更新排序
394397 }
395398 }
396399 } else {
0 commit comments