From ead32cb14dff66ea952988fd90b23f6a790a43ad Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 7 Jul 2016 22:21:04 +0300 Subject: [PATCH] added link caching... Signed-off-by: Alex A. Naanou --- wiki.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/wiki.js b/wiki.js index 4584668..28a0a3e 100755 --- a/wiki.js +++ b/wiki.js @@ -47,6 +47,11 @@ var Wiki = { __default_page__: 'DefaultPage', __templates__: 'Templates', + __wiki_link__: RegExp('('+[ + '[A-Z][a-z0-9]+[A-Z\/][a-zA-Z0-9\/]*', + '\\[[^\\]]+\\]', + ].join('|') +')', 'g'), + // current location... get location(){ @@ -99,27 +104,24 @@ var Wiki = { // - aquire default page (same order as above) // get text(){ - // get the page directly... - return (this.__wiki_data[this.location] || {}).text - // acquire the page from path... - || (this.acquire(this.title) || {}).text - // acquire the default page... - || (this.acquire(this.__default_page__) || {}).text - // nothing found... - || '' - }, + return (this.acquireData() || {}).text || '' }, set text(value){ var l = this.location this.__wiki_data[l] = this.__wiki_data[l] || {} this.__wiki_data[l].text = value + + // cache links... + this.__wiki_data[l].links = this.links }, - // XXX get links(){ + return (this.acquireData() || {}).links + || this.text.match(this.__wiki_link__) + // unwrap explicit links... + .map(e => e[0] == '[' ? e.slice(1, -1) : e) + // unique... + .filter((e, i, l) => l.slice(0, i).indexOf(e) == -1) }, - set links(value){ - }, - // XXX get list(){ @@ -168,6 +170,20 @@ var Wiki = { } }, + acquireData: function(path, title){ + path = path || this.path + title = title || this.title + + // get the page directly... + return this.__wiki_data[path +'/'+ title] + // acquire the page from path... + || this.acquire(title) + // acquire the default page... + || this.acquire(this.__default_page__) + // nothing found... + || null + }, + // serialization... json: function(path){