feat: Batching rename title processing

pull/7619/head
Ruslan Semak 8 months ago
parent 628d57d279
commit 4d42e5db04

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

Loading…
Cancel
Save