feat: truncate content exceeding the byte limit (#129)
Some checks failed
Publish Docker Image / publish (push) Has been cancelled
Some checks failed
Publish Docker Image / publish (push) Has been cancelled
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import * as core from '@actions/core'
|
||||
import * as fs from 'fs'
|
||||
import unicodeSubstring = require('unicode-substring')
|
||||
|
||||
export const README_FILEPATH_DEFAULT = './README.md'
|
||||
export const IMAGE_EXTENSIONS_DEFAULT = 'bmp,gif,jpg,jpeg,png,svg,webp'
|
||||
@@ -10,6 +11,8 @@ const REPOSITORY_URL = `${process.env['GITHUB_SERVER_URL']}/${process.env['GITHU
|
||||
const BLOB_PREFIX = `${REPOSITORY_URL}/blob/${process.env['GITHUB_REF_NAME']}/`
|
||||
const RAW_PREFIX = `${REPOSITORY_URL}/raw/${process.env['GITHUB_REF_NAME']}/`
|
||||
|
||||
const MAX_BYTES = 25000
|
||||
|
||||
type Rule = {
|
||||
/**
|
||||
* all left of the relative url belonging to the markdown image/link
|
||||
@@ -25,6 +28,14 @@ type Rule = {
|
||||
absUrlPrefix: string
|
||||
}
|
||||
|
||||
export function truncateToBytes(s: string, n: number): string {
|
||||
let len = n
|
||||
while (Buffer.byteLength(s) > n) {
|
||||
s = unicodeSubstring(s, 0, len--)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
export async function getReadmeContent(
|
||||
readmeFilepath: string,
|
||||
enableUrlCompletion: boolean,
|
||||
@@ -42,7 +53,14 @@ export async function getReadmeContent(
|
||||
imageExtensions
|
||||
)
|
||||
|
||||
return readmeContent
|
||||
const truncatedReadmeContent = truncateToBytes(readmeContent, MAX_BYTES)
|
||||
if (truncatedReadmeContent.length !== readmeContent.length) {
|
||||
core.warning(
|
||||
`The README content exceeds DockerHub's limit and has been truncated to ${MAX_BYTES} bytes.`
|
||||
)
|
||||
}
|
||||
|
||||
return truncatedReadmeContent
|
||||
}
|
||||
|
||||
export function completeRelativeUrls(
|
||||
|
||||
Reference in New Issue
Block a user