client: refactor linking and routing

Print all links through new uri.js component
Refactor the router to use more predictable parsing
Fix linking to entities with weird names (that contain slashes, + etc.)
This commit is contained in:
rr-
2017-01-20 21:51:04 +01:00
parent 6714f05b49
commit 1acceb941d
65 changed files with 380 additions and 295 deletions

View File

@ -1,6 +1,7 @@
'use strict';
const markdown = require('./markdown.js');
const uri = require('./uri.js');
function decamelize(str, sep) {
sep = sep === undefined ? '-' : sep;
@ -99,44 +100,10 @@ function formatInlineMarkdown(text) {
return markdown.formatInlineMarkdown(text);
}
function formatUrlParameters(dict) {
let result = [];
for (let key of Object.keys(dict)) {
const value = dict[key];
if (key === 'parameters') {
continue;
}
if (value) {
result.push(`${key}=${encodeURIComponent(value)}`);
}
}
return result.join(';');
}
function splitByWhitespace(str) {
return str.split(/\s+/).filter(s => s);
}
function parseUrlParameters(query) {
let result = {};
for (let word of (query || '').split(/;/)) {
const [key, value] = word.split(/=/, 2);
result[key] = value;
}
result.query = result.query || '';
result.page = parseInt(result.page || '1');
return result;
}
function parseUrlParametersRoute(ctx, next) {
// ctx.parameters = {"user":...,"action":...} from /users/:user/:action
// ctx.parameters.parameters = value of :parameters as per /url/:parameters
Object.assign(
ctx.parameters,
parseUrlParameters(ctx.parameters.parameters));
next();
}
function unindent(callSite, ...args) {
function format(str) {
let size = -1;
@ -232,9 +199,6 @@ function dataURItoBlob(dataURI) {
module.exports = {
range: range,
formatUrlParameters: formatUrlParameters,
parseUrlParameters: parseUrlParameters,
parseUrlParametersRoute: parseUrlParametersRoute,
formatRelativeTime: formatRelativeTime,
formatFileSize: formatFileSize,
formatMarkdown: formatMarkdown,