-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (84 loc) · 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { XMLParser, XMLBuilder } = require('fast-xml-parser')
const posthtml = require('posthtml')
const fs = require('fs')
const path = require('path')
const yargs = require('yargs')
const exRegExp = /(meta|instagram|facebook|инстаграм|мета|фейсбук)[а-яА-Я]*/gui
let gotMatchInsideMd = str => {
let m = str.match(/\[\/[a-zA-Z]\w*/)
if(m) {
let tagName = m[0].replace('[/', '')
let tagContent = str.match(new RegExp('(.*?)' + `\\/${tagName}`, 'ui')) || str.match(new RegExp(`\\[${tagName}` + '.*', 'ui'))
if(exRegExp.test(tagContent[1])) return true
}
return false
}
let replaceWithFootnote = (str, isTitle = false) => str.replace(exRegExp, (subStr, _, subStrIndex) => {
let nextIndex = subStrIndex + subStr.length
let nextSymbol = str[nextIndex]
if(
subStr.includes('вселен') ||
subStr.includes('vers')
) return subStr
let space = '<code style="letter-spacing: -7px;"> </code>'
let start = str[subStrIndex - 1] === ' ' ? space : ''
let end = nextSymbol === ' ' ? space : ''
let tooltipText =
subStr.includes('нстагра') || subStr.includes('ейсб') || subStr.includes('nstagr') || subStr.includes('aceboo')
? 'Продукт принадлежит организации, признанной экстремистской на территории Российской Федерации.'
: 'Организация признана экстремистской на территории Российской Федерации.'
let res = isTitle ? `${subStr}*` : `${start}[su_tooltip text="${tooltipText}" text_align="center"]${subStr}[/su_tooltip]${end}`
return res
})
let replaceTreeContentRecursively = node => {
if(node.content) {
if(node.content.length) {
if(node.content.length === 1 && typeof node.content[0] === 'string') {
node.content[0] = replaceWithFootnote(node.content[0], gotMatchInsideMd(node.content[0]))
} else {
for(let i = 0; i <= node.content.length - 1; i++) {
node.content[i] = replaceTreeContentRecursively(node.content[i])
}
}
}
} else if (typeof node === 'string') {
node = replaceWithFootnote(node, gotMatchInsideMd(node))
}
return node
}
let entry = async (fileName) => {
const data = fs.readFileSync(path.join(__dirname, fileName), 'utf8')
const parser = new XMLParser()
let jObj = parser.parse(data)
let items = !Array.isArray(jObj.rss.channel.item) ? [ jObj.rss.channel.item ] : jObj.rss.channel.item
for(let i = 0; i <= items.length - 1; i++) {
let title = items[i]['title']
let newTitle = replaceWithFootnote(title, true)
items[i]['title'] = newTitle
let metasArray = items[i]['wp:postmeta']
let newMetasArray = metasArray.map(m => {
if(m['wp:meta_key'] === '_crb_description' || m['wp:meta_key'] === '_crb_short_description') {
let newMeta = replaceWithFootnote(m['wp:meta_value'])
m['wp:meta_value'] = newMeta
}
return m
})
items[i]['wp:postmeta'] = newMetasArray
let content = items[i]['content:encoded']
let newContent = posthtml().use(tree => {
for(let i = 0; i <= tree.length - 1; i++) {
tree[i] = replaceTreeContentRecursively(tree[i])
}
return tree
}).process(content, { sync: true }).html
items[i]['content:encoded'] = newContent
}
const itemsBuilder = new XMLBuilder({ arrayNodeName: 'item' })
const xmlItems = itemsBuilder.build(items)
fs.writeFileSync(path.join(__dirname, `output_${fileName}`), xmlItems)
}
const options = yargs
.usage(`Usage: --fn <fileName>`)
.option('fn', { alias: 'fileName', describe: 'XML file name', type: 'string', demandOption: true })
.argv
entry(options.fileName)