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

Extension priority is not respected by the local provider #223

Closed
haakonmt opened this issue Aug 15, 2024 · 1 comment
Closed

Extension priority is not respected by the local provider #223

haakonmt opened this issue Aug 15, 2024 · 1 comment

Comments

@haakonmt
Copy link

haakonmt commented Aug 15, 2024

Local fonts are not sorted correctly during lookup. Looks to be because the filename utility used to find the file extension during the sort actually returns the filenames without the extension, and as such is unable to differ between them.

EDIT: Seemingly confirmed by this unit test: https://github.com/unjs/pathe/blob/main/test/utils.spec.ts#L61

Copy link

dosubot bot commented Aug 15, 2024

Hey @haakonmt! I'm here to assist you with any bugs, questions, or contributions you may have. Let me know how I can help while you wait for a human maintainer.

To resolve the issue where the extension priority is not respected by the local provider, causing local fonts to not be sorted correctly during lookup due to the filename utility returning filenames without the extension, you need to modify the lookupFont function. Specifically, ensure that the extension is correctly extracted and used for sorting.

Here's the corrected lookupFont function:

function lookupFont(family: string, suffixes: Array<string | number>): string[] {
  const slug = [fontFamilyToSlug(family), ...suffixes].join('-')
  const paths = providerContext.registry[slug]
  if (!paths || paths.length === 0) {
    return []
  }

  const fonts = new Set<string>()
  for (const path of paths) {
    const base = providerContext.rootPaths.find(root => path.startsWith(root))
    fonts.add(base ? withLeadingSlash(relative(base, path)) : path)
  }

  return [...fonts].sort((a, b) => {
    const extA = a.split('.').pop()!
    const extB = b.split('.').pop()!

    return extensionPriority.indexOf(extA) - extensionPriority.indexOf(extB)
  })
}

In this corrected version, the extA and extB variables are correctly assigned the file extensions by splitting the a and b strings, ensuring that the sorting respects the extension priority [1][2].

To continue talking to Dosu, mention @dosu.

danielroe added a commit to unjs/unifont that referenced this issue Oct 1, 2024
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

No branches or pull requests

1 participant