-
Notifications
You must be signed in to change notification settings - Fork 43
/
imgur-replace-image-target.js
37 lines (35 loc) · 1.46 KB
/
imgur-replace-image-target.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
const path = require('path')
const fs = require('fs')
const pagesDir = path.join(__dirname, 'articles', 'modules', 'ROOT', 'pages')
const imgurImageReplaceMacroRx = /(image::?)(https?:\/\/(:?i\.)?imgur.com\/([a-zA-Z0-9]+\.(png|jpe?g)))\[([^\]]*)]/g
;(async () => {
try {
const asciidocFiles = fs.readdirSync(pagesDir)
for (const file of asciidocFiles) {
if (file.endsWith('.adoc')) {
const filePath = path.join(pagesDir, file);
const content = fs.readFileSync(filePath, 'utf8')
const lines = content.split(/\r?\n/)
const data = []
for (const line of lines) {
data.push(line.replace(imgurImageReplaceMacroRx, (replace, ...args) => {
const imageMacro = args[0]
let imageName = args[3]
const fileExtension = args[4]
const attributesList = args[5]
const description = attributesList.split(',')[0]
if (description && description !== 'image' && !description.includes('=')) {
imageName = description.toLowerCase().replace(/\s/g, '-') + '.' + fileExtension
} else {
imageName = file.replace(/\.adoc$/, '') + '-' + imageName
}
return `${imageMacro}https://s3.amazonaws.com/dev.assets.neo4j.com/kb-content/${imageName}[${attributesList}]`
}))
}
fs.writeFileSync(filePath, data.join('\n'), 'utf8')
}
}
} catch (err) {
console.error('Error', err)
}
})()