Skip to content

Commit

Permalink
[ci] format
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored and astrobot-houston committed Jul 22, 2022
1 parent 7250e4e commit 4819e7b
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 41 deletions.
6 changes: 3 additions & 3 deletions packages/astro/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare module '*.md' {
export default load;
}

declare module "*.html" {
const Component: { render(opts: { slots: Record<string, string> }): string };
export default Component;
declare module '*.html' {
const Component: { render(opts: { slots: Record<string, string> }): string };
export default Component;
}
2 changes: 1 addition & 1 deletion packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import astroViteServerPlugin from '../vite-plugin-astro-server/index.js';
import astroVitePlugin from '../vite-plugin-astro/index.js';
import configAliasVitePlugin from '../vite-plugin-config-alias/index.js';
import envVitePlugin from '../vite-plugin-env/index.js';
import htmlVitePlugin from '../vite-plugin-html/index.js';
import astroIntegrationsContainerPlugin from '../vite-plugin-integrations-container/index.js';
import jsxVitePlugin from '../vite-plugin-jsx/index.js';
import htmlVitePlugin from '../vite-plugin-html/index.js';
import markdownVitePlugin from '../vite-plugin-markdown/index.js';
import astroScriptsPlugin from '../vite-plugin-scripts/index.js';
import { createCustomViteLogger } from './errors.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/vite-plugin-html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export default function html() {
async transform(source: string, id: string) {
if (!id.endsWith('.html')) return;
return await transform(source, id);
}
}
},
};
}
6 changes: 3 additions & 3 deletions packages/astro/src/vite-plugin-html/transform/escape.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Plugin } from 'unified';
import type { Root, RootContent } from 'hast';
import type MagicString from 'magic-string';
import type { Plugin } from 'unified';
import { visit } from 'unist-util-visit';

import { replaceAttribute, needsEscape, escape } from './utils.js';
import { escape, needsEscape, replaceAttribute } from './utils.js';

const rehypeEscape: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
return (tree, file) => {
Expand All @@ -17,7 +17,7 @@ const rehypeEscape: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
const newKey = needsEscape(key) ? escape(key) : key;
const newValue = needsEscape(value) ? escape(value) : value;
if (newKey === key && newValue === value) continue;
replaceAttribute(s, node, key, (value === '') ? newKey : `${newKey}="${newValue}"`);
replaceAttribute(s, node, key, value === '' ? newKey : `${newKey}="${newValue}"`);
}
}
});
Expand Down
21 changes: 10 additions & 11 deletions packages/astro/src/vite-plugin-html/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,25 @@ import slots, { SLOT_PREFIX } from './slots.js';
export async function transform(code: string, id: string) {
const s = new MagicString(code, { filename: id });
const imports = new Map();
const parser = rehype()
.data('settings', { fragment: true })
.use(escape, { s })
.use(slots, { s });
const parser = rehype().data('settings', { fragment: true }).use(escape, { s }).use(slots, { s });

const vfile = new VFile({ value: code, path: id })
await parser.process(vfile)
s.prepend(`export default {\n\t"astro:html": true,\n\trender({ slots: ${SLOT_PREFIX} }) {\n\t\treturn \``);
const vfile = new VFile({ value: code, path: id });
await parser.process(vfile);
s.prepend(
`export default {\n\t"astro:html": true,\n\trender({ slots: ${SLOT_PREFIX} }) {\n\t\treturn \``
);
s.append('`\n\t}\n}');

if (imports.size > 0) {
let importText = ''
let importText = '';
for (const [path, importName] of imports.entries()) {
importText += `import ${importName} from "${path}";\n`
importText += `import ${importName} from "${path}";\n`;
}
s.prepend(importText);
}

return {
code: s.toString(),
map: s.generateMap()
}
map: s.generateMap(),
};
}
24 changes: 13 additions & 11 deletions packages/astro/src/vite-plugin-html/transform/slots.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import type { Plugin } from 'unified';
import type { Root, RootContent } from 'hast';
import type { Plugin } from 'unified';

import { visit } from 'unist-util-visit';
import MagicString from 'magic-string';
import { visit } from 'unist-util-visit';
import { escape } from './utils.js';

const rehypeSlots: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
return (tree, file) => {
visit(tree, (node: Root | RootContent, index, parent) => {
if (node.type === 'element' && node.tagName === 'slot') {
return (tree, file) => {
visit(tree, (node: Root | RootContent, index, parent) => {
if (node.type === 'element' && node.tagName === 'slot') {
if (typeof node.properties?.['is:inline'] !== 'undefined') return;
const name = node.properties?.['name'] ?? 'default';
const start = node.position?.start.offset ?? 0;
const end = node.position?.end.offset ?? 0;
const first = node.children.at(0) ?? node;
const last = node.children.at(-1) ?? node;
const text = file.value.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0).toString();
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escape(text).trim()}\`}`)
}
});
}
}
const text = file.value
.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0)
.toString();
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escape(text).trim()}\`}`);
}
});
};
};

export default rehypeSlots;

Expand Down
10 changes: 6 additions & 4 deletions packages/astro/src/vite-plugin-html/transform/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ const splitAttrsTokenizer = /([\$\{\}\@a-z0-9_\:\-]*)\s*?=\s*?(['"]?)(.*?)\2\s+/

export function replaceAttribute(s: MagicString, node: Element, key: string, newValue: string) {
splitAttrsTokenizer.lastIndex = 0;
const text = s.original.slice(node.position?.start.offset ?? 0, node.position?.end.offset ?? 0).toString();
const text = s.original
.slice(node.position?.start.offset ?? 0, node.position?.end.offset ?? 0)
.toString();
const offset = text.indexOf(key);
if (offset === -1) return;
const start = node.position!.start.offset! + offset;
const tokens = text.slice(offset).split(splitAttrsTokenizer);
const token = tokens[0].replace(/([^>])(\>[\s\S]*$)/gmi, '$1');
const token = tokens[0].replace(/([^>])(\>[\s\S]*$)/gim, '$1');
if (token.trim() === key) {
const end = start + key.length;
s.overwrite(start, end, newValue)
s.overwrite(start, end, newValue);
} else {
const end = start + `${key}=${tokens[2]}${tokens[3]}${tokens[2]}`.length;
s.overwrite(start, end, newValue)
s.overwrite(start, end, newValue);
}
}
export function needsEscape(value: any): value is string {
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/test/html-escape.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('HTML Escape', () => {
expect(div.text()).to.equal('${foo}');

const span = $('span');
expect(span.attr('${attr}')).to.equal("");
expect(span.attr('${attr}')).to.equal('');

const ce = $('custom-element');
expect(ce.attr('x-data')).to.equal("`${test}`");
expect(ce.attr('x-data')).to.equal('`${test}`');

const script = $('script');
expect(script.text()).to.equal('console.log(`hello ${"world"}!`)');
Expand Down Expand Up @@ -57,10 +57,10 @@ describe('HTML Escape', () => {
expect(div.text()).to.equal('${foo}');

const span = $('span');
expect(span.attr('${attr}')).to.equal("");
expect(span.attr('${attr}')).to.equal('');

const ce = $('custom-element');
expect(ce.attr('x-data')).to.equal("`${test}`");
expect(ce.attr('x-data')).to.equal('`${test}`');

const script = $('script');
expect(script.text()).to.equal('console.log(`hello ${"world"}!`)');
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/test/html-page.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('HTML Page', () => {

it('works', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html)
const $ = cheerio.load(html);

const h1 = $('h1');

Expand All @@ -43,7 +43,7 @@ describe('HTML Page', () => {
expect(res.status).to.equal(200);

const html = await res.text();
const $ = cheerio.load(html)
const $ = cheerio.load(html);

const h1 = $('h1');

Expand Down

0 comments on commit 4819e7b

Please sign in to comment.