| 1 |
const utils = require( 'ext.discussionTools.init' ).utils; |
| 2 |
|
| 3 |
module.exports = {}; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
module.exports.overrideMwConfig = function ( config ) { |
| 12 |
Object.assign( |
| 13 |
mw.config.values, |
| 14 |
config |
| 15 |
); |
| 16 |
}; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
module.exports.getThreadContainer = function ( doc ) { |
| 25 |
|
| 26 |
|
| 27 |
const body = doc.body; |
| 28 |
const wrapper = body.querySelector( 'div.mw-parser-output' ); |
| 29 |
return wrapper || body; |
| 30 |
}; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
function getOffsetPath( ancestor, node, nodeOffset ) { |
| 41 |
const path = [ nodeOffset ]; |
| 42 |
while ( node !== ancestor ) { |
| 43 |
if ( node.parentNode === null ) { |
| 44 |
|
| 45 |
console.log( node, 'is not a descendant of', ancestor ); |
| 46 |
throw new Error( 'Not a descendant' ); |
| 47 |
} |
| 48 |
path.unshift( utils.childIndexOf( node ) ); |
| 49 |
node = node.parentNode; |
| 50 |
} |
| 51 |
return path.join( '/' ); |
| 52 |
} |
| 53 |
|
| 54 |
function getPathsFromRange( root, range ) { |
| 55 |
return [ |
| 56 |
getOffsetPath( root, range.startContainer, range.startOffset ), |
| 57 |
getOffsetPath( root, range.endContainer, range.endOffset ) |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
module.exports.serializeComments = function ( parent, root ) { |
| 68 |
if ( !parent.range.startContainer ) { |
| 69 |
|
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
delete parent.parent; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
parent.range = getPathsFromRange( root, parent.range ); |
| 79 |
if ( parent.signatureRanges ) { |
| 80 |
parent.signatureRanges = parent.signatureRanges.map( ( range ) => getPathsFromRange( root, range ) ); |
| 81 |
} |
| 82 |
if ( parent.timestampRanges ) { |
| 83 |
parent.timestampRanges = parent.timestampRanges.map( ( range ) => getPathsFromRange( root, range ) ); |
| 84 |
} |
| 85 |
if ( parent.timestamp ) { |
| 86 |
parent.timestamp = parent.getTimestampString(); |
| 87 |
} |
| 88 |
if ( !parent.displayName ) { |
| 89 |
delete parent.displayName; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
delete parent.rootNode; |
| 94 |
|
| 95 |
|
| 96 |
delete parent.authors; |
| 97 |
delete parent.commentCount; |
| 98 |
delete parent.oldestReply; |
| 99 |
delete parent.latestReply; |
| 100 |
|
| 101 |
parent.replies.forEach( ( comment ) => { |
| 102 |
module.exports.serializeComments( comment, root ); |
| 103 |
} ); |
| 104 |
}; |