From b0ac8218faddcda674932efb1970f58bcfea8173 Mon Sep 17 00:00:00 2001 From: Jaifroid Date: Wed, 13 Dec 2017 14:12:03 +0000 Subject: [PATCH] First attempt to support Stackexchange links Former-commit-id: dcc10e677e96be2748371b82780bb1b0e250290e [formerly 0ccd9321cf071e480335c69075f234b6c2dce8bf] Former-commit-id: dcfa5ab5e77ec6a77acaadc096419b2a3f96411e --- www/js/app.js | 24 ++++++++++-------------- www/js/lib/zimArchive.js | 3 ++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/www/js/app.js b/www/js/app.js index 1b9efc68..54004910 100644 --- a/www/js/app.js +++ b/www/js/app.js @@ -1783,19 +1783,15 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'abstractFile // Add an onclick event to go to this article // instead of following the link - if (url.substring(0, 2) === "./") { - url = url.substring(2); - } - // Remove the initial slash if it's an absolute URL - else if (url.substring(0, 1) === "/") { - url = url.substring(1); - } - //Supports stackexchange - else if (url.substring(0, 6) == "../../") { - url = url.substring(6); - //This should match a stackexchange URL and replace with short form - url = url.replace(/([^/]+\/\d+)\/[^/]+(\.html?)/, "$1$2"); - } + //Get rid of any absolute or relative prefixes (../, ./../, /../.., etc.) + url = url.replace(/^[.\/]*([\S\s]+)$/, "$1"); + + //Add namespace if there is an intermedial slash (supports Stackexchange) + //url = url.replace(/^([^\/]+\/[^\/]+[\S\s]+)$/, "A/$1"); + + //This experimentally shortens the URL for Stackexchange, but I'm not sure it's necessary... + //url = url.replace(/([^/]+\/\d+)\/[^/]+(\.html?)/, "A/$1$2"); + $(this).on('click', function (e) { clearFindInArticle(); //Re-enable top-level scrolling @@ -2284,7 +2280,7 @@ define(['jquery', 'zimArchiveLoader', 'util', 'uiUtil', 'cookies', 'abstractFile selectedArchive.getDirEntryByTitle(title).then(function(dirEntry) { if (dirEntry === null || dirEntry === undefined) { $("#readingArticle").hide(); - alert("Article with title " + title + " not found in the archive"); + console.error("Article with title " + title + " not found in the archive"); } else { $("#articleName").html(title); diff --git a/www/js/lib/zimArchive.js b/www/js/lib/zimArchive.js index 9d8302d8..86d19878 100644 --- a/www/js/lib/zimArchive.js +++ b/www/js/lib/zimArchive.js @@ -245,7 +245,8 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'], }); }; - var regexpTitleWithoutNameSpace = /^[^\/]+$/; + //Supports Stackexchange [kiwix-js #205] + var regexpTitleWithoutNameSpace = /^(?![\w-]\/[^\/]+?)/; /** * Searches a DirEntry (article / page) by its title.