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

fix: alt error mix with height/width #730

Closed
wants to merge 6 commits into from
Closed

Conversation

Mara-Li
Copy link
Contributor

@Mara-Li Mara-Li commented Jan 25, 2024

More granular detection of alt and resize in image

This was previously discuted into the discord troubleshooting channel.

Normally it shoulds support and detect size and alt text.

More granular detection of alt and resize in image
@benschlegel
Copy link
Collaborator

can you run npm run format please? should stop the checks from failing

Comment on lines 225 to 226
const dims = alias?.match(/^\d+(x\d+)?|\|\d+(x\d+)/) ? alias.match(/^\d+(x\d+)?|\|\d+(x\d+)/)![0] : ""
const alt = alias?.replace(dims, "")?.trim()
Copy link

@HeyItsGilbert HeyItsGilbert Jan 26, 2024

Choose a reason for hiding this comment

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

This would allow you to capture all the possible things in one match and return easier to identify groups.

const imgregex = new RegExp(
    // Test future changes https://rubular.com/r/KK6cVpqABINEfQ
    // Look for alt
    '^' + // Start from the beginning
    '(?<alt>' + // Start a named group for alt
        '(?!^\d+$)' + // This look ahead makes sure this isn't just digits
        '.*?' + // This searches for any character lazily.
    ')?' + // Close the group. The '?' means it may not be there.

    // Search for digits
    '(' + // Group the digits together
        '\|?' + // It may be proceeded by a pipe
        '\s+?' + // Handle any number of spaces after
        '(?<width>\d+)' + // Start a name group for width
        '(x(?<height>\d+))?' + // Height would require a 'x'. The '?' means it may not be given
    ')?' + // Close the digits w/ a '?' becuase it could only be alt text.
    '$' // Look until the end of the line
)
const matches= alias?.match(imgregex)
const width = matches.width ||= "auto"
const height= matches.height ||= "auto"
const alt= matches.alt ||= "auto"

Copy link
Contributor Author

@Mara-Li Mara-Li Jan 27, 2024

Choose a reason for hiding this comment

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

Failed to process content/Compendium/Ambre Loiseau.md: Invalid regular expression: /^(?(?!^d+$).*?)?(|?s?+(?d+)(x(?d+))?)?$/: Nothing to repeat

Tested in JS console and same error appear. Seams the regex have some problem in TS/JS

Copy link
Owner

Choose a reason for hiding this comment

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

@HeyItsGilbert ruby has a different regex engine from js, you might need to modify it (and test on https://regex101.com/)

Choose a reason for hiding this comment

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

Regex is valid but looks like capture groups aren't supported. https://www.regular-expressions.info/javascript.html

I'll give a different version when I have some time to update it.

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
Contributor Author

Choose a reason for hiding this comment

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

Seems this: https://regex101.com/r/lKh0xu/1 should works

Copy link

@HeyItsGilbert HeyItsGilbert Jan 27, 2024

Choose a reason for hiding this comment

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

This might be because I escaped characters to support /.../ syntax. That may not be needed for new RegEsp().

Copy link

@HeyItsGilbert HeyItsGilbert Jan 27, 2024

Choose a reason for hiding this comment

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

I think I found it. https://regex101.com/r/C8jPll/1

'\s?+' + // Handle any number of spaces after

Should be.

'\s+?' + // Handle any number of spaces after

The +? combo makes it 0 or more and the ? makes it optional. I think Javascript didn't like the alternative while other languages accept it. I updated the code above.

Copy link

@HeyItsGilbert HeyItsGilbert left a comment

Choose a reason for hiding this comment

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

This regex would allow a single match and returns capture groups. The rubular links shows a few test cases we tried on discord.

@jackyzha0
Copy link
Owner

superseded by 211f95c

@jackyzha0 jackyzha0 closed this Jan 28, 2024
@Mara-Li
Copy link
Contributor Author

Mara-Li commented Jan 28, 2024

Hello, this doesn't fix the current issue and the alt continue to be considered as width…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants