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 1 commit
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
17 changes: 12 additions & 5 deletions docs-utils/update-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ const fs = require('fs')
const path = require('path')
const frontmatter = require('front-matter')
const imageDownloader = require('image-downloader')
const { log } = require('console')
ricardoaerobr marked this conversation as resolved.
Show resolved Hide resolved

const baseURL = 'https://raw.githubusercontent.com/vtexdocs/help-center-content/main'
const baseURL = 'https://raw.githubusercontent.com/vtexdocs/help-center-content/main/'
const rootDir = path.resolve(__dirname, '..')
ricardoaerobr marked this conversation as resolved.
Show resolved Hide resolved

const isValidExtension = (ext) => {
Expand All @@ -22,26 +23,32 @@ const getExtension = (url) => {
}

const updateImages = async (filepath) => {
console.log("Running update images")
console.log(filepath)
ricardoaerobr marked this conversation as resolved.
Show resolved Hide resolved
const content = fs.readFileSync(filepath, 'utf-8')
const slug = frontmatter(content).attributes.slug
const locale = frontmatter(content).attributes.locale
ricardoaerobr marked this conversation as resolved.
Show resolved Hide resolved

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}`
console.log(newfilepath)
ricardoaerobr marked this conversation as resolved.
Show resolved Hide resolved
images.push({
filepath: path.resolve('images', locale, filename),
filepath: path.resolve(newfilepath),
url
})
console.log(images)

newURL = `${baseURL}/images/${locale}/${filename}`
newURL = `${baseURL}${filepath.split('.')[0]}_${imageIndex}.${ext}`
imageIndex++
} else if (path.isAbsolute(url)) {
newURL = `${baseURL}${url}`
} else {
Expand Down
Loading