Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download images for Help Center folder architecture #31

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions docs-utils/update-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const path = require('path')
const frontmatter = require('front-matter')
const imageDownloader = require('image-downloader')

const baseURL = 'https://raw.githubusercontent.com/vtexdocs/help-center-content/main'
const rootDir = path.resolve(__dirname, '..')
const baseURL = 'https://raw.githubusercontent.com/vtexdocs/help-center-content/main/'

const isValidExtension = (ext) => {
return /^[a-zA-Z0-9]*$/.test(ext)
Expand All @@ -23,31 +22,27 @@ const getExtension = (url) => {

const updateImages = async (filepath) => {
const content = fs.readFileSync(filepath, 'utf-8')
const slug = frontmatter(content).attributes.slug
const locale = frontmatter(content).attributes.locale

const images = []
let imageIndex = 1
const replaceURL = (match, extra, url) => {
const isMarkdownBlock = match.startsWith('![')
if (url.startsWith(baseURL)) return match

let newURL = ''
if (url.startsWith('http://') || url.startsWith('https://')) {
const ext = getExtension(url)
const filename = `${slug}-${images.length}.${ext}`


const newfilepath = `${filepath.split('.')[0]}_${imageIndex}.${ext}`
images.push({
filepath: path.resolve('images', locale, filename),
filepath: path.resolve(newfilepath),
url
})

newURL = `${baseURL}/images/${locale}/${filename}`
} else if (path.isAbsolute(url)) {
newURL = `${baseURL}${url}`
} else {
newURL = `${baseURL}${path.resolve(path.dirname(filepath), url).replace(rootDir, '')}`
}


newURL = `${baseURL}${filepath.split('.')[0]}_${imageIndex}.${ext}`
imageIndex++
}

return isMarkdownBlock ? `![${extra}](${newURL})` : `<img ${extra}src="${newURL}"`
}

Expand Down
Loading