Skip to content

Commit

Permalink
chore: logging cleanup (#11263)
Browse files Browse the repository at this point in the history
  • Loading branch information
wackbyte authored Jun 17, 2024
1 parent 6fcc246 commit 7d59750
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .changeset/thin-icons-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astrojs/sitemap': patch
'@astrojs/node': patch
'@astrojs/mdx': patch
---

Refactor to use Astro's integration logger for logging
22 changes: 16 additions & 6 deletions packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { markdownConfigDefaults } from '@astrojs/markdown-remark';
import type { AstroIntegration, ContainerRenderer, ContentEntryType, HookParameters } from 'astro';
import type {
AstroIntegration,
AstroIntegrationLogger,
ContainerRenderer,
ContentEntryType,
HookParameters,
} from 'astro';
import astroJSXRenderer from 'astro/jsx/renderer.js';
import type { Options as RemarkRehypeOptions } from 'remark-rehype';
import type { PluggableList } from 'unified';
Expand Down Expand Up @@ -75,7 +81,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
},
});
},
'astro:config:done': ({ config }) => {
'astro:config:done': ({ config, logger }) => {
// We resolve the final MDX options here so that other integrations have a chance to modify
// `config.markdown` before we access it
const extendMarkdownConfig =
Expand All @@ -84,7 +90,8 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
const resolvedMdxOptions = applyDefaultOptions({
options: partialMdxOptions,
defaults: markdownConfigToMdxOptions(
extendMarkdownConfig ? config.markdown : markdownConfigDefaults
extendMarkdownConfig ? config.markdown : markdownConfigDefaults,
logger
),
});

Expand All @@ -104,12 +111,15 @@ const defaultMdxOptions = {
optimize: false,
} satisfies Partial<MdxOptions>;

function markdownConfigToMdxOptions(markdownConfig: typeof markdownConfigDefaults): MdxOptions {
function markdownConfigToMdxOptions(
markdownConfig: typeof markdownConfigDefaults,
logger: AstroIntegrationLogger
): MdxOptions {
return {
...defaultMdxOptions,
...markdownConfig,
remarkPlugins: ignoreStringPlugins(markdownConfig.remarkPlugins),
rehypePlugins: ignoreStringPlugins(markdownConfig.rehypePlugins),
remarkPlugins: ignoreStringPlugins(markdownConfig.remarkPlugins, logger),
rehypePlugins: ignoreStringPlugins(markdownConfig.rehypePlugins, logger),
remarkRehype: (markdownConfig.remarkRehype as any) ?? {},
};
}
Expand Down
12 changes: 6 additions & 6 deletions packages/integrations/mdx/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Options as AcornOpts } from 'acorn';
import { parse } from 'acorn';
import type { AstroConfig, SSRError } from 'astro';
import type { AstroConfig, AstroIntegrationLogger, SSRError } from 'astro';
import matter from 'gray-matter';
import { bold, yellow } from 'kleur/colors';
import { bold } from 'kleur/colors';
import type { MdxjsEsm } from 'mdast-util-mdx';
import type { PluggableList } from 'unified';

Expand Down Expand Up @@ -85,22 +85,22 @@ export function jsToTreeNode(
};
}

export function ignoreStringPlugins(plugins: any[]): PluggableList {
export function ignoreStringPlugins(plugins: any[], logger: AstroIntegrationLogger): PluggableList {
let validPlugins: PluggableList = [];
let hasInvalidPlugin = false;
for (const plugin of plugins) {
if (typeof plugin === 'string') {
console.warn(yellow(`[MDX] ${bold(plugin)} not applied.`));
logger.warn(`${bold(plugin)} not applied.`);
hasInvalidPlugin = true;
} else if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
console.warn(yellow(`[MDX] ${bold(plugin[0])} not applied.`));
logger.warn(`${bold(plugin[0])} not applied.`);
hasInvalidPlugin = true;
} else {
validPlugins.push(plugin);
}
}
if (hasInvalidPlugin) {
console.warn(
logger.warn(
`To inherit Markdown plugins in MDX, please use explicit imports in your config instead of "strings." See Markdown docs: https://docs.astro.build/en/guides/markdown-content/#markdown-plugins`
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/integrations/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
},
});
},
'astro:config:done': ({ setAdapter, config }) => {
'astro:config:done': ({ setAdapter, config, logger }) => {
_options = {
...userOptions,
client: config.build.client?.toString(),
Expand All @@ -57,8 +57,8 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
setAdapter(getAdapter(_options));

if (config.output === 'static') {
console.warn(
`[@astrojs/node] \`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.`
logger.warn(
`\`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.`
);
}
},
Expand Down
10 changes: 1 addition & 9 deletions packages/integrations/sitemap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,7 @@ const createPlugin = (options?: SitemapOptions): AstroIntegration => {

const { filter, customPages, serialize, entryLimit } = opts;

let finalSiteUrl: URL;
if (config.site) {
finalSiteUrl = new URL(config.base, config.site);
} else {
console.warn(
'The Sitemap integration requires the `site` astro.config option. Skipping.'
);
return;
}
let finalSiteUrl = new URL(config.base, config.site);
const shouldIgnoreStatus = isStatusCodePage(Object.keys(opts.i18n?.locales ?? {}));
let pageUrls = pages
.filter((p) => !shouldIgnoreStatus(p.pathname))
Expand Down

0 comments on commit 7d59750

Please sign in to comment.