Post by joedf » 09 Aug 2019, 09:42
New/Current version:
https://www.autohotkey.com/boards/assets/prism/prism.urls.js
Now it simply puts one "click" event per code box (instead of an anchor tag for each token) and detects the token type and produces a link that opens in a new tab/window. CSS is used for "link" styling for the tokens.
Code: Select all
// NEW VERSION
// Using global events for target case-by-case detection
// linker to docs for prism.js update
// created 7/01/2019 - updated 09/08/2019
// script by joedf
var redirectQURL = '/docs/redirect.php?topic=';
// add CSS 'link' styling
codelinkstyle = document.createElement('style');
codelinkstyle .innerHTML = "code .selector:hover, code .constant:hover, code .keyword:hover, code .symbol:hover, \
code .tag:hover, code .builtin:hover, code .x_function:hover, code .important:hover, \
code .variable:hover { \
text-decoration:underline !important; \
cursor: pointer; \
}";
document.head.appendChild(codelinkstyle);
// add events for each codebox
var cboxes = document.querySelectorAll('code.language-autohotkey');
for(var i=0;i<cboxes.length;i++) {
cboxes[i].addEventListener('click', function(e){
var citem = e.target;
if (citem.className.indexOf('token') >= 0) {
var type = citem.className.replace('token','').trim().toLowerCase();
var cURL = null;
// supported types: selector, constant, operator, and variable
if (['selector','constant','keyword','symbol','tag'].indexOf(type)>=0) {
cURL = redirectQURL+citem.innerText;
} else if (['builtin','x_function'].indexOf(type)>-1) {
cURL = redirectQURL+citem.innerText+'()';
} else if (type.indexOf('variable')>-1) {
// same as bove normal rediretor usage but remove the % symbols...
cURL = redirectQURL+citem.innerText.replace(/%/gi,'');
} else if (type.indexOf('important')>-1) {
// same as bove normal rediretor usage but remove the # symbols...
cURL = redirectQURL+citem.innerText.replace(/#/gi,'');
} else {
// do nothing? leave as is...
return false;
}
var w = window.open(cURL, '_blank');
w.focus();
return true;
}
});
}
console.log("prism.urls.js executed.");
Old version:
Code: Select all
// ORIGINAL
// with <a>nchor tags on each thing
// linker to docs for prism.js update
// created 7/01/2019 - updated 09/08/2019
// script by joedf
////////////////////////////////////
var redirectQURL = '/docs/redirect.php?topic=';
// get AHK codeboxes
var cboxes = document.querySelectorAll('code.language-autohotkey');
for(var i=0;i<cboxes.length;i++) {
['mouseenter', 'touchstart'].forEach(function(e) {
cboxes[i].addEventListener(e,function(){
if (!this.hasAttribute('highlightedlinks')) {
ctokens = this.querySelectorAll('.token:not(.punctuation):not(.comment)');
this.setAttribute('highlightedlinks','true');
for(var j=0;j<ctokens.length;j++) {
(function(index) {
setTimeout(function(){
var citem = ctokens[index];
var type = citem.className.replace('token','').trim().toLowerCase();
// supported types: selector, constant, operator, and variable
if (['selector','constant','keyword','symbol','tag'].indexOf(type)>-1) {
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML+'">'+citem.outerHTML+'</a>';
} else if (['builtin','x_function'].indexOf(type)>-1) {
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML+'()">'+citem.outerHTML+'</a>';
} else if (type.indexOf('variable')>-1) {
// same as bove normal rediretor usage but remove the % symbols...
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML.replace(/%/gi,'')+'">'+citem.outerHTML+'</a>';
} else if (type.indexOf('important')>-1) {
// same as bove normal rediretor usage but remove the # symbols...
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML.replace(/#/gi,'')+'">'+citem.outerHTML+'</a>';
} else {
// do nothing? leave as is...
}
},1);
})(j);
}
}
});
});
}
/* force the underlined link style */
codelinkstyle = document.createElement('style');
codelinkstyle .innerHTML = "code a:hover{text-decoration:underline !important;}";
document.head.appendChild(codelinkstyle);
console.log("prism.urls.js executed.");
[b]New/Current version[/b]: https://www.autohotkey.com/boards/assets/prism/prism.urls.js
Now it simply puts one "click" event per code box (instead of an anchor tag for each token) and detects the token type and produces a link that opens in a new tab/window. CSS is used for "link" styling for the tokens.
[Codebox=javascript file=prism.urls.js]// NEW VERSION
// Using global events for target case-by-case detection
// linker to docs for prism.js update
// created 7/01/2019 - updated 09/08/2019
// script by joedf
var redirectQURL = '/docs/redirect.php?topic=';
// add CSS 'link' styling
codelinkstyle = document.createElement('style');
codelinkstyle .innerHTML = "code .selector:hover, code .constant:hover, code .keyword:hover, code .symbol:hover, \
code .tag:hover, code .builtin:hover, code .x_function:hover, code .important:hover, \
code .variable:hover { \
text-decoration:underline !important; \
cursor: pointer; \
}";
document.head.appendChild(codelinkstyle);
// add events for each codebox
var cboxes = document.querySelectorAll('code.language-autohotkey');
for(var i=0;i<cboxes.length;i++) {
cboxes[i].addEventListener('click', function(e){
var citem = e.target;
if (citem.className.indexOf('token') >= 0) {
var type = citem.className.replace('token','').trim().toLowerCase();
var cURL = null;
// supported types: selector, constant, operator, and variable
if (['selector','constant','keyword','symbol','tag'].indexOf(type)>=0) {
cURL = redirectQURL+citem.innerText;
} else if (['builtin','x_function'].indexOf(type)>-1) {
cURL = redirectQURL+citem.innerText+'()';
} else if (type.indexOf('variable')>-1) {
// same as bove normal rediretor usage but remove the % symbols...
cURL = redirectQURL+citem.innerText.replace(/%/gi,'');
} else if (type.indexOf('important')>-1) {
// same as bove normal rediretor usage but remove the # symbols...
cURL = redirectQURL+citem.innerText.replace(/#/gi,'');
} else {
// do nothing? leave as is...
return false;
}
var w = window.open(cURL, '_blank');
w.focus();
return true;
}
});
}
console.log("prism.urls.js executed.");
[/Codebox]
[b]Old version[/b]:
[Codebox=javascript file=prism.urls.js]// ORIGINAL
// with <a>nchor tags on each thing
// linker to docs for prism.js update
// created 7/01/2019 - updated 09/08/2019
// script by joedf
////////////////////////////////////
var redirectQURL = '/docs/redirect.php?topic=';
// get AHK codeboxes
var cboxes = document.querySelectorAll('code.language-autohotkey');
for(var i=0;i<cboxes.length;i++) {
['mouseenter', 'touchstart'].forEach(function(e) {
cboxes[i].addEventListener(e,function(){
if (!this.hasAttribute('highlightedlinks')) {
ctokens = this.querySelectorAll('.token:not(.punctuation):not(.comment)');
this.setAttribute('highlightedlinks','true');
for(var j=0;j<ctokens.length;j++) {
(function(index) {
setTimeout(function(){
var citem = ctokens[index];
var type = citem.className.replace('token','').trim().toLowerCase();
// supported types: selector, constant, operator, and variable
if (['selector','constant','keyword','symbol','tag'].indexOf(type)>-1) {
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML+'">'+citem.outerHTML+'</a>';
} else if (['builtin','x_function'].indexOf(type)>-1) {
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML+'()">'+citem.outerHTML+'</a>';
} else if (type.indexOf('variable')>-1) {
// same as bove normal rediretor usage but remove the % symbols...
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML.replace(/%/gi,'')+'">'+citem.outerHTML+'</a>';
} else if (type.indexOf('important')>-1) {
// same as bove normal rediretor usage but remove the # symbols...
citem.outerHTML = '<a href="'+redirectQURL+citem.innerHTML.replace(/#/gi,'')+'">'+citem.outerHTML+'</a>';
} else {
// do nothing? leave as is...
}
},1);
})(j);
}
}
});
});
}
/* force the underlined link style */
codelinkstyle = document.createElement('style');
codelinkstyle .innerHTML = "code a:hover{text-decoration:underline !important;}";
document.head.appendChild(codelinkstyle);
console.log("prism.urls.js executed.");
[/Codebox]