thinking of how to resolve he relative url base issue (see: pwiki2.js)

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-08-10 15:29:45 +03:00
parent e0f9b7eaa2
commit b810c625c9
5 changed files with 98 additions and 34 deletions

View File

@ -35,7 +35,6 @@ store.update('System',
var pwiki = var pwiki =
module.pwiki = module.pwiki =
// XXX // XXX
//page.DOMPage('/', '/', store)
//page.Page('/', '/', store) //page.Page('/', '/', store)
page.pWikiPageElement('/', '/', store) page.pWikiPageElement('/', '/', store)

View File

@ -25,7 +25,11 @@ var relProxy =
function(name){ function(name){
var func = function(path='.', ...args){ var func = function(path='.', ...args){
return this.store[name]( return this.store[name](
/* XXX RELATIVE
pwpath.relative(this.location+'/', path),
/*/
pwpath.relative(this.location, path), pwpath.relative(this.location, path),
//*/
...args) } ...args) }
Object.defineProperty(func, 'name', {value: name}) Object.defineProperty(func, 'name', {value: name})
return func } return func }
@ -36,7 +40,11 @@ function(name){
strict = path strict = path
path = '.' } path = '.' }
return this.store[name]( return this.store[name](
/* XXX RELATIVE
pwpath.relative(this.location+'/', path),
/*/
pwpath.relative(this.location, path), pwpath.relative(this.location, path),
//*/
strict) } strict) }
Object.defineProperty(func, 'name', {value: name}) Object.defineProperty(func, 'name', {value: name})
return func } return func }
@ -161,10 +169,10 @@ object.Constructor('BasePage', {
// XXX should these be writable??? // XXX should these be writable???
get name(){ get name(){
return pwpath.split(this.path).pop() }, return pwpath.basename(this.location) },
//set name(value){ }, //set name(value){ },
get dir(){ get dir(){
return pwpath.relative(this.location, '..') }, return pwpath.dirname(this.location) },
//set dir(value){ }, //set dir(value){ },
get isPattern(){ get isPattern(){
return this.location.includes('*') }, return this.location.includes('*') },
@ -215,7 +223,11 @@ object.Constructor('BasePage', {
__update__: function(data){ __update__: function(data){
return this.store.update(this.location, data) }, return this.store.update(this.location, data) },
__delete__: function(path='.'){ __delete__: function(path='.'){
/* XXX RELATIVE
return this.store.delete(pwpath.relative(this.location+'/', path)) },
/*/
return this.store.delete(pwpath.relative(this.location, path)) }, return this.store.delete(pwpath.relative(this.location, path)) },
//*/
// page data... // page data...
// //
@ -292,7 +304,11 @@ object.Constructor('BasePage', {
strict = path strict = path
path = '.' } path = '.' }
return this.store.find( return this.store.find(
/* XXX RELATIVE
//pwpath.relative(this.location+'/', path), strict) },
/*/
pwpath.relative(this.location, path), strict) }, pwpath.relative(this.location, path), strict) },
//*/
// //
// .get(<path>[, <data>]) // .get(<path>[, <data>])

View File

@ -98,10 +98,34 @@ module = {
.join('/'), .join('/'),
'string') }, 'string') },
basename: function(path){ basename: function(path){
return this.split(path).pop() }, path = this.split(path)
return path.length == 1 ?
path[0]
: (path.at(-1) == '' ?
path.at(-2)
: path.at(-1)) },
dirname: function(path){ dirname: function(path){
return this.relative(path, '..', 'string') }, path = this.split(path)
path = path.length == 1 ?
'.'
: path.length == 2 ?
path[0]
: (path.at(-1) == '' ?
path.slice(0, -2)
: path.slice(0, -1))
.join('/')
return path == '' ?
'/'
: path },
// XXX BUG? which is more correct??
// .relative('a/b/c', 'x')
// -> 'a/b/c/x' (current)
// or:
// .relative('a/b/c', 'x')
// -> 'a/b/x'
// ...not sure about this yet...
// XXX REVISE...
relative: function(parent, path, format='auto'){ relative: function(parent, path, format='auto'){
format = format == 'auto' ? format = format == 'auto' ?
(path instanceof Array ? (path instanceof Array ?
@ -118,6 +142,13 @@ module = {
path = path instanceof Array ? path = path instanceof Array ?
path path
: path.split(/\s*[\\\/]+\s*/) : path.split(/\s*[\\\/]+\s*/)
// NOTE: relative paths are siblings and not children unless the
// parent is explicitly a directory (i.e. ends in '/')...
/* XXX RELATIVE -- leading @ in path is the same as a trailing / in parent...
path[0] == '@' ?
path.shift()
: parent.pop()
//*/
return this.normalize([...parent, ...path], format) }, return this.normalize([...parent, ...path], format) },
// Build alternative paths for page acquisition... // Build alternative paths for page acquisition...
@ -172,7 +203,9 @@ module = {
while(base.length > 0){ while(base.length > 0){
var p = base.slice() var p = base.slice()
while(p.length > 0){ while(p.length > 0){
var cur = this.relative(p, tpl +'/'+ pg, 'string') // NOTE: we are adding '' to path here to get things
// relative to it and not relative to basedir...
var cur = this.relative([...p, ''], tpl +'/'+ pg, 'string')
if(!seen.has(cur)){ if(!seen.has(cur)){
seen.add(cur) seen.add(cur)
yield cur } yield cur }

View File

@ -98,24 +98,39 @@ require(['./browser'], function(pwiki){
evt.preventDefault() evt.preventDefault()
var [path, hash] = location.hash.slice(1).split('#') var [path, hash] = location.hash.slice(1).split('#')
path = path.trim() == '' ? path = path.trim() == '' ?
'/' pwiki.path
//'/'
: path : path
// XXX treat links as absolute unless explicitly relative...
path = /^\.\.?([\\\/].*)?$/.test(path) ?
path
: '/'+path
// NOTE: .hash needs to be set before .path, otherwise the path
// handlers will not see it..
pwiki.hash = hash
pwiki.path = path }) pwiki.path = path })
pwiki pwiki
.onNavigate(function(){ .onNavigate(function(){
// NOTE: we do not need to directly update location.hash here as // NOTE: we do not need to directly update location.hash here as
// that will push an extra history item... // that will push an extra history item...
history.replaceState({path: pwiki.path}, pwiki.title, '#'+pwiki.path) history.replaceState(
{path: this.path},
this.title,
'#'+this.path
+(this.hash ?
'#'+this.hash
: ''))
// NOTE: we are intentionally not awaiting for this -- this // NOTE: we are intentionally not awaiting for this -- this
// separates the navigate and load events... // separates the navigate and load events...
pwiki.refresh() }) this.refresh() })
.onLoad(function(evt){ .onLoad(function(evt){
// handle title... // handle title...
document.querySelector('title').innerHTML = this.title document.querySelector('title').innerHTML = this.title
// scroll to anchor element...
// XXX when loaded us <elem>.scrollIntoView() to get to hash... this.hash
// ...would also be nice to keep hash within location.hash... && this.dom
}) .querySelector('#'+ this.hash)
.scrollIntoView() })
// show current page... // show current page...
pwiki.path = location.hash.slice(1) pwiki.path = location.hash.slice(1)

View File

@ -1,15 +1,23 @@
/********************************************************************** /**********************************************************************
* *
* *
* XXX wikiword filter seems to act up on / * XXX BUG: .get('/*').raw hangs...
* XXX BUG? /test/wikiword -- produces nested links... * XXX RELATIVE relative urls are a bit odd...
* to reproduce: * Path/to/page opens Moo -> Path/to/Page/Moo
* await p.pwiki.get('/test/wikiword').text * should be (???):
* ..as-is, seems not to deal well with tags... * Path/to/page opens Moo -> Path/to/Moo
* ...also might be a good idea to try and moke wiki-word generation * this boils down to how path.relative(..) works, treating the base
* a DOM thing instead of a filter, this seems logical as we might * as a directory always (current) vs. only if '/' is at the end, on
* need it only within a UI and I do not think I'll be working on * one hand the current approach is more uniform with less subtle ways
* a non DOM interface (curses, ...) any time soon... * to make mistakes but on the other hand this may introduce a lot
* of complexity to the user writing links, e.g. how should the
* following be interpreted?
* page: /SomePage
* link: SomeOtherPage
* -> /SomeOtherPage
* -> /SomePage/SomeOtherPage (current)
* the current approach does not seem to be intuitive...
* can this be fixed uniformly across the whole system???
* XXX add action to reset overloaded (bootstrap) pages... * XXX add action to reset overloaded (bootstrap) pages...
* - per page * - per page
* - global * - global
@ -29,18 +37,11 @@
* - migrate bootstrap * - migrate bootstrap
* - store topology * - store topology
* - markdown -- DONE?? * - markdown -- DONE??
* - WikiWord -- DONE-ish * - WikiWord -- DONE
* currently this is broken as it does not know how to deal with HTML * - dom filters -- DONE
* this can be solved by one of: * - wikiword -- DONE
* - make this a dom filter and only handle text nodes (as v1-2) * - path2link (wikiword?) --
* - add a way to go around tags (as pwiki/parser) * - editor
* the first approach looks more promising...
* - dom filters ???
* does this need to be a pWiki level thing or just a js call/load??
* ...this can be used to contain all to page-side stuff like:
* - hash handling / navigation
* - editors
* - wikiwords
* - configuration * - configuration
* - defaults * - defaults
* - System/config (global) * - System/config (global)