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

feat: Convert buffer image to PNG format #17

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"download": "^8.0.0",
"gray-matter": "^4.0.3",
"mkdirp": "^2.1.3",
"sharp": "^0.33.1",
"yaml": "^2.2.1"
},
"devDependencies": {
Expand Down
14 changes: 13 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import download from 'download'
import dayjs from 'dayjs'
import {extractImages} from './extract-images'
import {formatFrontMatterValue} from './format'
import sharp from 'sharp'

async function run(): Promise<void> {
const token: string = core.getInput('token')
Expand Down Expand Up @@ -103,11 +104,22 @@ async function run(): Promise<void> {
let bodyText = bodyWithoutFrontMatter
const images = extractImages(bodyText)
for (const image of images) {
const newImageFilename = path.basename(image.filename)
let newImageFilename = path.basename(image.filename)
fs.writeFileSync(
path.join(dirname, newImageFilename),
await download(image.filename)
)

const validExtensionsRegex = /\.(jpg|jpeg|png)$/i
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"확장자가 없으면.." 이라기보다 "저 셋 중에 하나로 끝나지 않으면" 의 조건으로 작업을 하신 거 같은데, 어떤 게 맞으려나요? 혹시 .tiff 라던가 이런 확장자의 파일들도 다 png 로 변환을 해줘야 하는 걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 헷갈리게 썼네요 ㅠㅠ 이미지 확장자만 확인하는 의미로 작성 했습니다..!!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JEM1224 아 그러면 저 좀더 궁금한 게, 위의 세 확장자가 아니라 다른 이미지, 예를 들면 .tiff, .gif 이런 것들도 다 아래 if 분기를 타게 돼서 png 로 변환될텐데, 그래도 되는 건지...? 아니면 확장자가 아예 없는 경우에만 아래 분기를 타야 하는건지? 어떤 게 맞을까요?


if (!validExtensionsRegex.test(image.filename)) {
await sharp(`./${newImageFilename}`)
.toFormat('png')
.toFile(`${newImageFilename}.png`)
const dirpath = path.join(dirname, newImageFilename)
newImageFilename += '.png'
fs.unlinkSync(dirpath)
}
bodyText = bodyText.replace(
image.match,
`![${image.alt}](./${newImageFilename}${
Expand Down