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: New getFontInfo function #70

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

alexmensch
Copy link

The purpose of adding this functionality is to allow download management of font files outside of the google-fonts-helper package. I want more control over when font files are fetched using caching so that font files aren't downloaded into my Eleventy dev project unnecessarily often.

Changes:

  • Added new extractFontInfo method to Downloader class.
  • Added getFontInfo function export to access method.
  • Updated README with example

Here's an example of how I'm using this in an Eleventy project.

Global data file:

export default {
  css: async function () {
    return localCSS;
  },
  files: async function () {
    const fonts = [];

    for (const [url, fileName] of fontMaps) {
      const fontBuffer = await EleventyFetch(url, {
        duration: "1w",
        type: "buffer",
      });
      fonts.push({fontBuffer, fileName});
    }
    return fonts;
  },
  buildFontPath: buildFontPath,
  buildCSS: buildCSS,
};

.eleventy.js config:

// Write configured Google Fonts to build output
  eleventyConfig.on("eleventy.after",
    async () => {
      const fontBuffers = await fonts.files();

      for (const { fontBuffer, fileName } of fontBuffers) {
        const outputPath = path.join('_site', fonts.buildFontPath, fileName);
        const outputDir = path.dirname(outputPath);
        
        await fs.mkdir(outputDir, { recursive: true });
        await fs.writeFile(outputPath, fontBuffer);
      }
    }
  );

@ricardogobbosouza
Copy link
Member

Hi @alexmensch
Thanks for the PR
It's a very specific case, we can improve the Downloader class to be able to meet this case.
I'll make some changes to the PR

@alexmensch
Copy link
Author

Hi @alexmensch Thanks for the PR It's a very specific case, we can improve the Downloader class to be able to meet this case. I'll make some changes to the PR

Amazing, thank you very much for the consideration! I know it will make a big impact as 11ty has a huge usage base, and you've built a really nice library.

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.

2 participants