Skip to content

Commit c814122

Browse files
committed
cleanup for ID extraction from images
* No need to support object embeds. All our SVGs are embedded as IMG. * Make use of src attribute to always get a fully qualified URL * use searchParams to access media variable * fix for rewriting detection when running from a sub directory * simpler rewrite handling
1 parent 445b1fb commit c814122

File tree

2 files changed

+42
-55
lines changed

2 files changed

+42
-55
lines changed

script.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
jQuery( function() {
1+
jQuery(function () {
22
/* DOKUWIKI:include script/helpers.js */
33
/* DOKUWIKI:include script/service.js */
44
/* DOKUWIKI:include script/elements.js */
55

66
// add diagram edit button to all SVGs included in wiki pages
7-
const $images = jQuery( 'img, object' ).filter( '.media, .medialeft, .mediacenter, .mediaright' );
7+
const $images = jQuery('img').filter('.media, .medialeft, .mediacenter, .mediaright');
88

99
// collect image IDs with file extension
1010
const imageIds = $images.map(function (key, image) {
11-
return extractIdFromMediaUrl(image.currentSrc);
11+
return extractIdFromMediaUrl(image.src);
1212
}).toArray();
1313

1414
let ajaxData = {};
@@ -18,21 +18,18 @@ jQuery( function() {
1818
// callback to attach buttons to editable diagrams
1919
const attachButtons = function (result) {
2020
const diagrams = JSON.parse(result);
21-
$images.each( function() {
22-
const current = jQuery( this );
23-
// FIXME what is the difference?
24-
const src = this.nodeName === 'OBJECT' ? current.attr( 'data' ) : current.attr( 'src' );
25-
26-
const id = extractIdFromMediaUrl(src);
21+
$images.each(function () {
22+
const id = extractIdFromMediaUrl(this.src);
23+
const $current = jQuery(this);
2724
if (diagrams.includes(id)) {
2825
let $editButton = editDiagramButton(id);
29-
if( current.parent()[0].nodeName === 'A' ) {
30-
current.parent().after( "<br>", $editButton );
26+
if ($current.parent()[0].nodeName === 'A') {
27+
$current.parent().after("<br>", $editButton);
3128
} else {
32-
current.after( "<br>", $editButton );
29+
$current.after("<br>", $editButton);
3330
}
3431
}
35-
} );
32+
});
3633
};
3734

3835
// query backend about permissions and SVG properties before attaching edit buttons
@@ -63,10 +60,10 @@ jQuery( function() {
6360
width: 600,
6461
appendTo: '.dokuwiki',
6562
modal: true,
66-
open: () => {
63+
open: function () {
6764
const ns = isMMPage ? jQuery('.panelHeader h3 strong').html() : jQuery('#media__ns').html();
6865
jQuery('#drawio__current-ns').text(ns);
69-
},
66+
}
7067
});
7168
});
7269
$mm_tree.prepend($createLink);
@@ -80,11 +77,11 @@ jQuery( function() {
8077
const targetNode = $df[0];
8178

8279
// observe the target node descendants
83-
const config = { childList: true, subtree: true };
80+
const config = {childList: true, subtree: true};
8481

8582
// add edit diagram button to file actions
86-
const addEditButton = function(mutationsList, observer) {
87-
for(let mutation of mutationsList) {
83+
const addEditButton = function (mutationsList, observer) {
84+
for (let mutation of mutationsList) {
8885
// div.file has been filled with new content (detail view)
8986
if (mutation.type === 'childList') {
9087
const $svgLink = jQuery('a.mf_svg');
@@ -105,4 +102,4 @@ jQuery( function() {
105102
const observer = new MutationObserver(addEditButton);
106103
observer.observe(targetNode, config);
107104
});
108-
} );
105+
});

script/helpers.js

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const doctypeXML = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w
88
* @returns {boolean}
99
*/
1010
function validId(id) {
11-
return id.length > 0 && /^[\w][\w\.\-]*$/.test( id )
11+
return id.length > 0 && /^[\w][\w\.\-]*$/.test(id)
1212
}
1313

1414
/**
@@ -46,46 +46,36 @@ function splitFullId(fullId) {
4646
* @returns {string}
4747
*/
4848
function extractIdFromMediaUrl(url) {
49-
// handle empty base url
50-
if (url.indexOf('http') !== 0 && DOKU_BASE === '/') {
51-
url = window.location.origin + url;
52-
}
53-
5449
const urlObj = new URL(url);
55-
const path = urlObj.pathname;
56-
const href = urlObj.href;
50+
let path = urlObj.pathname;
51+
if (path.indexOf(DOKU_BASE) === 0) {
52+
path = path.substr(DOKU_BASE.length);
53+
}
5754

58-
if (path.indexOf('/lib/exe/detail.php/') === 0 || path.indexOf('/lib/exe/fetch.php/') === 0) {
59-
// media with internal rewriting
60-
const mediaIdMatch = new RegExp(
61-
'(?:\\/lib\\/exe\\/detail.php\\/|\\/lib\\/exe\\/fetch.php\\/)([^&]+)$'
62-
);
63-
const matches = mediaIdMatch.exec(path);
64-
if (matches[1]) {
65-
return normalizeId(matches[1]);
66-
}
67-
} else if (path.indexOf('/lib/exe/detail.php') === 0 || path.indexOf('/lib/exe/fetch.php') === 0) {
68-
// media without rewriting
69-
const mediaIdMatch = new RegExp('(?:media=)([^&]+)');
70-
const matches = mediaIdMatch.exec(href);
71-
if (matches[1]) {
72-
return normalizeId(matches[1]);
73-
}
74-
} else if (path.indexOf('/_media/') === 0) { // media with .htaccess rewriting
75-
const mediaIdMatch = /(?:_media\/)([^&\?]+)/;
76-
const matches = href.match(mediaIdMatch);
77-
if (matches[1]) {
78-
return normalizeId(matches[1]);
79-
}
80-
} else if (path.indexOf('/_detail/') === 0) { // media with .htaccess rewriting
81-
const mediaIdMatch = /(?:_detail\/)([^&\?]+)/;
82-
const matches = href.match(mediaIdMatch);
83-
if (matches[1]) {
84-
return normalizeId(matches[1]);
85-
}
55+
if (urlObj.searchParams.get('media') !== null) {
56+
// no rewriting
57+
return normalizeId(urlObj.searchParams.get('media'));
58+
} else if (
59+
path.indexOf('lib/exe/detail.php/') === 0 ||
60+
path.indexOf('lib/exe/fetch.php/') === 0
61+
) {
62+
// internally rewritten URL, cut off three segments
63+
return normalizeId(path.split('/').slice(3).join(':'));
64+
} else if (
65+
path.indexOf('_media/') === 0 ||
66+
path.indexOf('_detail/') === 0
67+
) {
68+
// .htaccess rewritten URL, cut off one segment
69+
return normalizeId(path.split('/').slice(1).join(':'));
8670
}
8771
}
8872

73+
/**
74+
* Handles IDs with useslash enabled
75+
*
76+
* @param {string} id
77+
* @return {string}
78+
*/
8979
function normalizeId(id) {
9080
return ':' + id.replace(/\//g, ":");
9181
}

0 commit comments

Comments
 (0)