mirror of https://gitgud.io/fatchan/jschan.git
quotes cross-thread fixes #11
parent
956a8693ec
commit
8ce5aa292c
3 changed files with 50 additions and 8 deletions
@ -0,0 +1,46 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
const Posts = require(__dirname+'/../db-models/posts.js') |
||||||
|
, quoteRegex = /^>>\d+/gm; |
||||||
|
|
||||||
|
module.exports = async (board, text) => { |
||||||
|
|
||||||
|
//get the matches
|
||||||
|
const matches = text.match(quoteRegex); |
||||||
|
if (!matches) { |
||||||
|
return text; |
||||||
|
} |
||||||
|
|
||||||
|
//get all the Ids
|
||||||
|
const quoteIds = matches.map(x => +x.substring(2)); |
||||||
|
|
||||||
|
//get all posts with those Ids
|
||||||
|
const posts = await Posts.getPosts(board, quoteIds, false); |
||||||
|
|
||||||
|
//turn the result into a map of postId => threadId/postId
|
||||||
|
const postThreadObject = {}; |
||||||
|
let validQuotes = 0; |
||||||
|
for (let i = 0; i < posts.length; i++) { |
||||||
|
const post = posts[i]; |
||||||
|
postThreadObject[post.postId] = post.thread || post.postId; |
||||||
|
validQuotes++; |
||||||
|
} |
||||||
|
|
||||||
|
console.log(validQuotes); |
||||||
|
//if none of the quotes were real, dont do a replace
|
||||||
|
if (validQuotes === 0) { |
||||||
|
return text; |
||||||
|
} |
||||||
|
|
||||||
|
//then replace the quotes with only ones that exist
|
||||||
|
text = text.replace(quoteRegex, (match) => { |
||||||
|
const quotenum = +match.substring(2); |
||||||
|
if (postThreadObject[quotenum]) { |
||||||
|
return `<a class='quote' href='/${board}/thread/${postThreadObject[quotenum]}#${quotenum}'>>>${quotenum}</a>`; |
||||||
|
} |
||||||
|
return match; |
||||||
|
}); |
||||||
|
|
||||||
|
return text; |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue