Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README-ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface RenderParams<Data, Plugins> {
icon?: Icon;
// nonce to be set on the appropriate tags
nonce?: string;
// base tag attributes
base?: Base;

// common options
// Page title
Expand Down Expand Up @@ -85,6 +87,32 @@ interface RenderParams<Data, Plugins> {
}
```

### `Base`

Описывает тег `base`:

```typescript
interface Base {
href?: string;
target?: HTMLBaseElement['target'];
}
```

Например:

```js
renderLayout({
title: 'Home page',
base: {target: '_top'},
});
```

Будет выглядеть следующим образом:

```html
<base target="_top" />
```

### `Meta`

Описывает тег `meta`:
Expand Down Expand Up @@ -252,6 +280,7 @@ interface CommonOptions {
}

export interface HeadContent {
base?: Base;
scripts: Script[];
helpers: RenderHelpers;
links: Link[];
Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ interface RenderParams<Data, Plugins> {
icon?: Icon;
// nonce to be set on the appropriate tags
nonce?: string;
// base tag attributes
base?: Base;

// common options
// Page title
Expand Down Expand Up @@ -85,6 +87,32 @@ interface RenderParams<Data, Plugins> {
}
```

### Base

Describes `base` tag:

```typescript
interface Base {
href?: string;
target?: HTMLBaseElement['target'];
}
```

Example:

```js
renderLayout({
title: 'Home page',
base: {target: '_top'},
});
```

Will be rendered as:

```html
<base target="_top" />
```

### Meta

Describes `meta` tag:
Expand Down Expand Up @@ -252,6 +280,7 @@ interface CommonOptions {
}

export interface HeadContent {
base?: Base;
scripts: Script[];
helpers: RenderHelpers;
links: Link[];
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export {
} from './plugins/index.js';

export type {
Base,
Plugin,
Icon,
Link,
Expand Down
18 changes: 18 additions & 0 deletions src/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
return {
name: 'dirPlugin',
apply({renderContent: {htmlAttributes}}) {
htmlAttributes.dir = 'ltr';

Check warning on line 9 in src/render.test.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Assignment to property of function parameter 'htmlAttributes'
},
};
}
Expand All @@ -16,6 +16,24 @@
expect(createRenderFunction([dirPlugin()])({title: 'Foobar'})).toMatch('<html dir="ltr">');
});

test('should render `<base>` tag with target', () => {
const html = createRenderFunction()({title: 'Foobar', base: {target: '_top'}});
expect(html).toMatch('<base target="_top">');
});

test('should render `<base>` tag with href and target', () => {
const html = createRenderFunction()({
title: 'Foobar',
base: {href: 'https://example.com/', target: '_top'},
});
expect(html).toMatch('<base href="https://example.com/" target="_top">');
});

test('should not render `<base>` tag when base is not provided', () => {
const html = createRenderFunction()({title: 'Foobar'});
expect(html).not.toMatch('<base');
});

test('should render root content', () => {
expect(createRenderFunction()({title: 'Foobar'})).toMatch(/<div id="root">\s*<\/div>/);
expect(createRenderFunction()({title: 'Foobar', bodyContent: {root: 'content'}})).toMatch(
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@

export type Meta = {name: string; content: string};

export interface Base {
href?: string;
target?: HTMLBaseElement['target'];
}

export interface CommonOptions {
title: string;
lang?: string;
isMobile?: boolean;
}

export interface HeadContent {
base?: Base;
Comment thread
ValeraS marked this conversation as resolved.
scripts: Script[];
helpers: RenderHelpers;
links: Link[];
Expand All @@ -56,7 +62,7 @@
afterRoot: string[];
}

export interface RenderContent extends HeadContent {
export interface RenderContent extends Required<HeadContent> {
htmlAttributes: Attributes;
bodyContent: BodyContent;
}
Expand All @@ -70,7 +76,7 @@
renderLink(link: Link): string;
attrs(obj: Attributes): string;
}
export interface Plugin<Options = any, Name extends string = string> {

Check warning on line 79 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
name: Name;
apply: (params: {
options: Options | undefined;
Expand All @@ -84,6 +90,7 @@
data?: Data;
icon?: Icon;
nonce?: string;
base?: Base;
// content
htmlAttributes?: Attributes;
meta?: Meta[];
Expand All @@ -110,7 +117,7 @@
? {[K in Name & string]: Options}
: {};

type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void

Check warning on line 120 in src/types.ts

View workflow job for this annotation

GitHub Actions / Verify Files

Unexpected any. Specify a different type
? I
: never;

Expand Down
4 changes: 3 additions & 1 deletion src/utils/generateRenderContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ export function generateRenderContent<Plugins extends Plugin[], Data>(
links.unshift({rel: 'icon', type: icon.type, sizes: icon.sizes, href: icon.href});
}

const {lang, isMobile, title, pluginsOptions = {}} = params;
const {lang, isMobile, title, base = {}, pluginsOptions = {}} = params;
for (const plugin of plugins ?? []) {
plugin.apply({
options: hasProperty(pluginsOptions, plugin.name)
? pluginsOptions[plugin.name]
: undefined,
renderContent: {
base,
htmlAttributes,
meta,
links,
Expand All @@ -99,6 +100,7 @@ export function generateRenderContent<Plugins extends Plugin[], Data>(
}

return {
base,
htmlAttributes,
meta,
links,
Expand Down
16 changes: 14 additions & 2 deletions src/utils/renderHeadContent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import type {HeadContent} from '../types.js';

export function renderHeadContent(content: HeadContent): string {
const {scripts, helpers, links, meta, styleSheets, inlineStyleSheets, inlineScripts, title} =
content;
const {
base,
scripts,
helpers,
links,
meta,
styleSheets,
inlineStyleSheets,
inlineScripts,
title,
} = content;

const baseAttrs = base ? helpers.attrs({...base}) : '';

return `
<meta charset="utf-8">
${baseAttrs ? `<base ${baseAttrs}>` : ''}
<title>${title}</title>
${[
...scripts.map(({src, crossOrigin}) =>
Expand Down
Loading