Skip to content

Commit

Permalink
Merge pull request #122 from withastro/main
Browse files Browse the repository at this point in the history
A58
  • Loading branch information
akshit20421 authored Jan 29, 2024
2 parents c3f38ef + 44c957f commit 69ebbd6
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-colts-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/markdown-remark": patch
---

Fixes a bug where non-UTF-8 file names are not displayed when using relative paths in markdowns.
5 changes: 5 additions & 0 deletions .changeset/large-kangaroos-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes a bug in `Astro.currentLocale` where the value was incorrectly computed during the build.
5 changes: 5 additions & 0 deletions .changeset/silent-buckets-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes an issue where `astro:i18n` could not be used in framework components.
2 changes: 1 addition & 1 deletion examples/blog/src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SITE_TITLE, SITE_DESCRIPTION } from '../consts';
<BaseHead title={SITE_TITLE} description={SITE_DESCRIPTION} />
</head>
<body>
<Header title={SITE_TITLE} />
<Header />
<main>
<h1>🧑‍🚀 Hello, Astronaut!</h1>
<p>
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export const SUPPORTED_MARKDOWN_FILE_EXTENSIONS = [

// The folder name where to find the middleware
export const MIDDLEWARE_PATH_SEGMENT_NAME = 'middleware';

export const ROUTE_DATA_SYMBOL = 'astro.routeData';
10 changes: 8 additions & 2 deletions packages/astro/src/core/render/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import { AstroError, AstroErrorData } from '../errors/index.js';
import type { Environment } from './environment.js';
import { getParamsAndProps } from './params-and-props.js';
import type { RoutingStrategies } from '../config/schema.js';
import { ROUTE_DATA_SYMBOL } from '../constants.js';

const clientLocalsSymbol = Symbol.for('astro.locals');
const routeDataSymbol = Symbol.for(ROUTE_DATA_SYMBOL);

/**
* The RenderContext represents the parts of rendering that are specific to one request.
Expand Down Expand Up @@ -243,8 +245,12 @@ export function computeCurrentLocale(
routingStrategy: RoutingStrategies | undefined,
defaultLocale: string | undefined
): undefined | string {
const requestUrl = new URL(request.url);
for (const segment of requestUrl.pathname.split('/')) {
const routeData: RouteData | undefined = Reflect.get(request, routeDataSymbol);
if (!routeData) {
return defaultLocale;
}

for (const segment of routeData.route.split('/')) {
for (const locale of locales) {
if (typeof locale === 'string') {
if (normalizeTheLocale(locale) === normalizeTheLocale(segment)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/i18n/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import type { Locales, MiddlewareHandler, RouteData, SSRManifest } from '../@typ
import type { PipelineHookFunction } from '../core/pipeline.js';
import { getPathByLocale, normalizeTheLocale } from './index.js';
import { shouldAppendForwardSlash } from '../core/build/util.js';
import { ROUTE_DATA_SYMBOL } from '../core/constants.js';

const routeDataSymbol = Symbol.for('astro.routeData');
const routeDataSymbol = Symbol.for(ROUTE_DATA_SYMBOL);

// Checks if the pathname has any locale, exception for the defaultLocale, which is ignored on purpose.
function pathnameHasLocale(pathname: string, locales: Locales): boolean {
Expand Down
34 changes: 12 additions & 22 deletions packages/astro/src/i18n/vite-plugin-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ import { AstroError } from '../core/errors/errors.js';
import { AstroErrorData } from '../core/errors/index.js';

const virtualModuleId = 'astro:i18n';
const configId = 'astro-internal:i18n-config';
const resolvedConfigId = `\0${configId}`;

type AstroInternationalization = {
settings: AstroSettings;
};

export interface I18nInternalConfig
extends Pick<AstroConfig, 'base' | 'site' | 'trailingSlash'>,
NonNullable<AstroConfig['i18n']>,
Pick<AstroConfig['build'], 'format'> {}
Pick<AstroConfig['build'], 'format'> {
i18n: AstroConfig['i18n'];
}

export default function astroInternationalization({
settings,
Expand All @@ -29,28 +28,19 @@ export default function astroInternationalization({
return {
name: 'astro:i18n',
enforce: 'pre',
async resolveId(id) {
config(config) {
const i18nConfig: I18nInternalConfig = { base, format, site, trailingSlash, i18n };
return {
define: {
__ASTRO_INTERNAL_I18N_CONFIG__: JSON.stringify(i18nConfig),
},
};
},
resolveId(id) {
if (id === virtualModuleId) {
if (i18n === undefined) throw new AstroError(AstroErrorData.i18nNotEnabled);
return this.resolve('astro/virtual-modules/i18n.js');
}
if (id === configId) return resolvedConfigId;
},
load(id) {
if (id === resolvedConfigId) {
const { defaultLocale, locales, routing, fallback } = i18n!;
const config: I18nInternalConfig = {
base,
format,
site,
trailingSlash,
defaultLocale,
locales,
routing,
fallback,
};
return `export default ${JSON.stringify(config)};`;
}
},
};
}
6 changes: 3 additions & 3 deletions packages/astro/src/virtual-modules/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as I18nInternals from '../i18n/index.js';
import type { I18nInternalConfig } from '../i18n/vite-plugin-i18n.js';
export { normalizeTheLocale, toCodes, toPaths } from '../i18n/index.js';

// @ts-expect-error
import config from 'astro-internal:i18n-config';
const { trailingSlash, format, site, defaultLocale, locales, routing } =
config as I18nInternalConfig;
const { trailingSlash, format, site, i18n } = __ASTRO_INTERNAL_I18N_CONFIG__ as I18nInternalConfig;
const { defaultLocale, locales, routing } = i18n!;
const base = import.meta.env.BASE_URL;

export type GetLocaleOptions = I18nInternals.GetLocaleOptions;
Expand Down
6 changes: 4 additions & 2 deletions packages/astro/test/astro-assets-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ describe('Assets Prefix - Static', () => {
it('markdown image src start with assetsPrefix', async () => {
const html = await fixture.readFile('/markdown/index.html');
const $ = cheerio.load(html);
const imgAsset = $('img');
expect(imgAsset.attr('src')).to.match(assetsPrefixRegex);
const imgAssets = $('img');
imgAssets.each((i, el) => {
expect(el.attribs.src).to.match(assetsPrefixRegex);
});
});

it('content collections image src start with assetsPrefix', async () => {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
Relative image has assetsPrefix

![Relative image](../assets/penguin1.jpg)

![non-UTF-8 file name image](../assets/ペンギン.jpg)
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
const currentLocale = Astro.currentLocale;
---

<html>
<head>
<title>Astro</title>
</head>
<body>
Oi essa e start
Oi essa e start: {currentLocale}
</body>
</html>
2 changes: 1 addition & 1 deletion packages/astro/test/i18n-routing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ describe('[SSG] i18n routing', () => {
it('should render localised page correctly', async () => {
let html = await fixture.readFile('/pt/start/index.html');
let $ = cheerio.load(html);
expect($('body').text()).includes('Oi essa e start');
expect($('body').text()).includes('Oi essa e start: pt');

html = await fixture.readFile('/pt/blog/1/index.html');
$ = cheerio.load(html);
Expand Down
2 changes: 2 additions & 0 deletions packages/markdown/remark/src/rehype-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function rehypeImages() {
if (node.tagName !== 'img') return;

if (node.properties?.src) {
node.properties.src = decodeURI(node.properties.src);

if (file.data.imagePaths?.has(node.properties.src)) {
const { ...props } = node.properties;

Expand Down

0 comments on commit 69ebbd6

Please sign in to comment.