feat: Removed batching rename title processing

pull/7619/head
Ruslan Semak 8 months ago
parent 4378ef91fe
commit 5d6d43ae57

@ -395,51 +395,48 @@ module.exports = class Page extends Model {
const allPages = await WIKI.models.pages.query() const allPages = await WIKI.models.pages.query()
const mentionedInPages = allPages.filter(page => page.content.includes(ogPage.path)) const mentionedInPages = allPages.filter(page => page.content.includes(ogPage.path))
const batch = 3 for (const page of mentionedInPages) {
const iterations = Math.ceil(mentionedInPages.length / batch) // const page = mentionedInPages.find(page => page.title.includes('Шаблон'))
for (let i = 0; i < iterations; i++) { const $ = cheerio.load(page.content)
const batchPages = mentionedInPages.slice(i * batch, i * batch + batch)
await Promise.all(batchPages.map(async (page) => { const targetLinks = $(`a[href="/${ogPage.path}"]`)
const $ = cheerio.load(page.content)
const targetLinks = $(`a[href="/${ogPage.path}"]`) let changed = false
let changed = false targetLinks.each((_, el) => {
const $el = $(el)
targetLinks.each((_, el) => { // Получаем все дочерние узлы (включая текстовые)
const $el = $(el) const contents = $el.contents()
const contents = $el.contents() // Рекурсивная функция для поиска и замены текста
const replaceText = (nodes) => {
const replaceText = (nodes) => { nodes.each((_, node) => {
nodes.each((_, node) => { if (node.type === 'text') {
if (node.type === 'text') { node.data = opts.title
node.data = opts.title changed = true
changed = true } else if (node.children) {
} else if (node.children) { replaceText($(node).contents())
replaceText($(node).contents()) }
} })
}) }
}
replaceText(contents) replaceText(contents)
}) })
if (changed) { if (changed) {
page.content = $.html() page.content = $.html()
await WIKI.models.pages.query().patch({ await WIKI.models.pages.query().patch({
content: page.content content: page.content
}).where('id', page.id) }).where('id', page.id)
WIKI.logger.info(`Упоминание "${ogPage.title}" заменено на "${opts.title}" на странице "${page.title}" (/${page.path})`) WIKI.logger.info(`Упоминание "${ogPage.title}" заменено на "${opts.title}" на странице "${page.title}" (/${page.path})`)
await WIKI.models.pages.renderPage(page) await WIKI.models.pages.renderPage(page)
WIKI.events.outbound.emit('deletePageFromCache', page.hash) WIKI.events.outbound.emit('deletePageFromCache', page.hash)
} }
}))
} }
} }

Loading…
Cancel
Save