Skip to content

Commit

Permalink
support embedding DiscussionTools comment links
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus-Rost committed Apr 23, 2024
1 parent 506858c commit ea59a23
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
38 changes: 32 additions & 6 deletions functions/parse_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const removeClasses = [
'.noexcerpt',
'.sortkey',
'.mw-collapsible.mw-collapsed',
'.ext-discussiontools-init-replylink-buttons',
'span.sprite-file img',
'.c-item-hoverbox__display',
'wb\\:sectionedit'
Expand Down Expand Up @@ -136,7 +137,8 @@ export default function parse_page(lang, msg, content, embed, wiki, reaction, {n
if ( ns === 8 ) {
title = title.split(':').slice(1).join(':');
if ( title.endsWith( '/' + pagelanguage ) ) title = title.substring(0, title.length - ( pagelanguage.length + 1 ));
return got.get( wiki + 'api.php?action=query&meta=allmessages&amprop=default&amincludelocal=true&amlang=' + encodeURIComponent( pagelanguage ) + '&ammessages=' + encodeURIComponent( title ) + '&format=json', {
else if ( pagelanguage === 'en' ) pagelanguage = '';
return got.get( wiki + 'api.php?action=query&meta=allmessages&amprop=default&amincludelocal=true' + ( pagelanguage ? '&amlang=' + encodeURIComponent( pagelanguage ) : '' ) + '&ammessages=' + encodeURIComponent( title ) + '&format=json', {
timeout: {
request: 10_000
},
Expand Down Expand Up @@ -451,14 +453,32 @@ export default function parse_page(lang, msg, content, embed, wiki, reaction, {n
if ( fragment && sectionLength && embed.length < ( 5_990 - sectionLength ) ) {
let newFragment = '';
let exactMatch = true;
let allSections = $('h1, h2, h3, h4, h5, h6').children('span');
let allSections = $('h1, h2, h3, h4, h5, h6').children('span').not('.mw-editsection, .mw-editsection-like');
var section = allSections.filter( (i, span) => {
return ( '#' + span.attribs.id === toSection(fragment, wiki.spaceReplacement) );
} ).parent();
var sectionContent;
if ( !section.length ) {
section = $('[id="' + toSection(fragment, wiki.spaceReplacement, false).replace( '#', '' ) + '"]');
newFragment = section.attr('id');
if ( section.is(':empty') ) {
if ( section.is('[data-mw-comment-start]') ) {
let start = section.parent();
let end = $('[data-mw-comment-end="'+newFragment+'"]');
end.nextAll().remove();
let allSections = $('h1, h2, h3, h4, h5, h6, .mw-heading');
section = allSections.nextUntil(allSections).has(end).last().prevUntil(allSections).last().prev();
if ( !section.length ) section = $('<h1>').html(response.body.parse.displaytitle);
if ( start.is(end.parent()) ) sectionContent = $('<div>').append(start);
else {
let begin = start.parentsUntil(end.parents()).last();
let last = end.parents().filter(begin.siblings());
let mid = begin.nextUntil(last);
start.parentsUntil(begin).prevAll().remove();
end.parentsUntil(last).nextAll().remove();
sectionContent = $('<div>').append(begin, mid, last);
}
}
else if ( section.is(':empty') ) {
section = section.parent();
if ( ['h1','h2','h3','h4','h5','h6'].includes( section.prev()[0]?.tagName ) ) {
section = section.prev();
Expand Down Expand Up @@ -501,12 +521,18 @@ export default function parse_page(lang, msg, content, embed, wiki, reaction, {n
section = section.first();
var sectionLevel = section[0].tagName.replace('h', '');
if ( !['1','2','3','4','5','6'].includes( sectionLevel ) ) sectionLevel = '10';
let headerList = ['h1','h2','h3','h4','h5','h6'];
let headerList = [
'h1, div.mw-heading1',
'h2, div.mw-heading2',
'h3, div.mw-heading3',
'h4, div.mw-heading4',
'h5, div.mw-heading5',
'h6, div.mw-heading6'
];
if ( section.parent('div.mw-heading').length ) {
section = section.parent();
headerList = ['div.mw-heading1','div.mw-heading2','div.mw-heading3','div.mw-heading4','div.mw-heading5','div.mw-heading6'];
}
var sectionContent = $('<div>').append(
sectionContent ??= $('<div>').append(
section.nextUntil(headerList.slice(0, sectionLevel).join(', '))
);
section.find('div, ' + removeClasses.join(', ')).remove();
Expand Down
2 changes: 1 addition & 1 deletion util/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function htmlToDiscord(html, pagelink = '', ...escapeArgs) {
text += '**';
}
if ( !pagelink ) return;
if ( tagname === 'a' && attribs.href && !classes.includes( 'new' ) && /^(?:(?:https?:)?\/\/|\/|#)/.test(attribs.href) ) {
if ( tagname === 'a' && attribs.href && !classes.includes( 'new' ) && !classes.includes( 'ext-discussiontools-init-timestamplink' ) && /^(?:(?:https?:)?\/\/|\/|#)/.test(attribs.href) ) {
try {
if ( relativeFix && /^\/(?!\/)/.test(attribs.href) ) attribs.href = relativeFix(attribs.href, pagelink);
href = new URL(attribs.href, pagelink).href.replace( /[()]/g, '\\$&' );
Expand Down

0 comments on commit ea59a23

Please sign in to comment.