updating links on page move done...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2016-07-09 06:26:16 +03:00
parent 83defb8b50
commit 02bf51ca96

131
wiki.js
View File

@ -67,58 +67,119 @@ var Wiki = {
// NOTE: changing this will move the page to the new path and change // NOTE: changing this will move the page to the new path and change
// .location acordingly... // .location acordingly...
// NOTE: same applies to path parts below... // NOTE: same applies to path parts below...
//
// XXX use a template for the redirect page...
get path(){ get path(){
return this.location }, return this.location },
set path(value){ set path(value){
var l = this.location
// old title...
var ot = this.title
value = normalizePath(value) value = normalizePath(value)
var l = this.location
if(value == l){
return
}
// old...
var otitle = this.title
var odir = this.dir
if(this.exists(l)){ if(this.exists(l)){
this.__wiki_data[value] = this.__wiki_data[l] this.__wiki_data[value] = this.__wiki_data[l]
} }
this.location = value this.location = value
// new title... // new...
var nt = this.title var ntitle = this.title
var ndir = this.dir
// update all links... var redirect = false
// update links to this page...
this.pages(function(page){ this.pages(function(page){
page.location != l && page.links.forEach(lnk => { // skip the old page...
var p = path2lst(lnk) if(page.location == l){
var t = p.pop() return
}
page.text = page.text.replace(page.__wiki_link__, function(lnk){
var from = lnk[0] == '[' ? lnk.slice(1, -1) : lnk
// 1:1 change // get path/title...
(lnk == l var p = path2lst(from)
// change all links that resolve to current page var t = p.pop()
// with same basename... p = normalizePath(p)
|| (t == ot && page.acquire(p, t) == l)
// XXX need to take changing paths into acount... var target = page.acquire(p, t)
|| true) // page target changed...
&& console.log(lnk, '->', p +'/'+ nt) // NOTE: this can happen either when a link was an orphan
/* // or if the new page path shadowed the original
&& (page.text.match(this.__wiki_link__) || []) // target...
// get both types of links... // XXX should we report the exact condition here???
.filter(e => e == l || e == '['+l+']') if(target == value){
// unique... console.log('Link target changed:', lnk, '->', value)
.filter((e, i, l) => l.slice(0, i).indexOf(e) == -1) return lnk
// do the replacing...
.forEach(e => // skip links that do not resolve to target...
page.text = page.text.replace( } else if(page.acquire(p, t) != l){
// XXX BUG: this will change anything return lnk
// containing the pattern... }
RegExp(''
// regexp-quote string... // format the new link...
+e.replace(/[\[\]\(\)\$\^\{\}\?\|\*\.\+]/g, '\\$1') var to = p == '' ? ntitle : p +'/'+ ntitle
+ '', 'g'), to = lnk[0] == '[' ? '['+to+'}' : to
value))
//*/ // explicit link change -- replace...
if(from == l){
//console.log(lnk, '->', to)
return to
// path did not change -- change the title...
} else if(ndir == odir){
// conflict: the new link will not resolve to the
// target page...
if(page.acquire(p, ntitle) != value){
console.log('ERR:', lnk, '->', to,
'is shadowed by:', page.acquire(p, ntitle))
// XXX should we add a note to the link???
redirect = true
// replace title...
} else {
//console.log(lnk, '->', to)
return to
}
// path changed -- keep link + add redirect page...
} else {
redirect = true
}
// no change...
return lnk
}) })
}) })
// redirect...
//
// XXX should we use a template here???
// ...might be a good idea to set a .redirect attr and either
// do an internal/transparent redirect or show a redirect
// template
// ...might also be good to add an option to fix the link from
// the redirect page...
if(redirect){
console.log('CREATING REDIRECT PAGE:', l, '->', value, '')
this.__wiki_data[l].text = 'REDIRECT TO: ' + value
+'<br>'
+'<br><i>NOTE: This page was created when renaming the target '
+'page that resulted new link being broken (i.e. resolved '
+'to a different page from the target)</i>'
this.__wiki_data[l].redirect = value
// cleaup... // cleaup...
delete this.__wiki_data[l] } else {
delete this.__wiki_data[l]
}
}, },
// path parts: directory... // path parts: directory...