-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
239 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import { defineIntegration } from 'astro-integration-kit'; | ||
import { createResolver, defineIntegration } from 'astro-integration-kit'; | ||
import { z } from 'astro/zod'; | ||
import { debug } from './internal/debug.js'; | ||
|
||
export default defineIntegration({ | ||
name: '@inox-tools/portal-gun', | ||
optionsSchema: z.never().optional(), | ||
setup() { | ||
// TODO: Implement this | ||
}, | ||
name: '@inox-tools/portal-gun', | ||
optionsSchema: z.never().optional(), | ||
setup() { | ||
const { resolve } = createResolver(import.meta.url); | ||
|
||
return { | ||
hooks: { | ||
'astro:config:setup': (params) => { | ||
debug('Injecting middleware'); | ||
params.addMiddleware({ | ||
order: 'pre', | ||
entrypoint: resolve('./runtime/middleware.js'), | ||
}); | ||
}, | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { MiddlewareHandler } from 'astro'; | ||
import { rehype } from 'rehype'; | ||
import type * as hast from 'hast'; | ||
import * as visitor from 'unist-util-visit'; | ||
import { debug } from '../internal/debug.js'; | ||
|
||
const processor = rehype(); | ||
|
||
export const onRequest: MiddlewareHandler = async (_, next) => { | ||
const response = await next(); | ||
if (response.headers.get('content-type')?.includes('text/html') !== true) { | ||
return response; | ||
} | ||
|
||
const body = await response.text(); | ||
const tree = processor.parse(body); | ||
|
||
const portalContents = new Map<string, hast.ElementContent[]>(); | ||
|
||
visitor.visit(tree, 'element', (node, index, parent) => { | ||
if (node.tagName !== 'portal') return visitor.CONTINUE; | ||
|
||
const target = node.properties?.to; | ||
if (typeof target !== 'string') return visitor.CONTINUE; | ||
|
||
debug(`Sending ${node.children.length} children to portal ${target}`); | ||
|
||
const children = portalContents.get(target) ?? []; | ||
children.push(...node.children); | ||
portalContents.set(target, children); | ||
|
||
if (parent && index !== undefined) { | ||
parent.children.splice(index, 1); | ||
|
||
// Continue to the same index, which is now the following element | ||
return [visitor.CONTINUE, index]; | ||
} | ||
}); | ||
|
||
visitor.visit(tree, 'element', (node, index, parent) => { | ||
if (!parent || index === undefined) return visitor.CONTINUE; | ||
if ( | ||
!(node.tagName === 'portal' || (node.tagName === 'link' && node.properties?.as === 'portal')) | ||
) | ||
return visitor.CONTINUE; | ||
|
||
let name = node.tagName === 'portal' ? node.properties?.name : node.properties?.rel; | ||
if (Array.isArray(name)) { | ||
name = name[0]; | ||
} | ||
if (typeof name !== 'string') return visitor.CONTINUE; | ||
|
||
const children = portalContents.get(name) ?? []; | ||
|
||
debug(`Receiving ${children.length} children into portal ${name}`); | ||
|
||
parent.children.splice(index, 1, ...children); | ||
}); | ||
|
||
const newBody = processor.stringify(tree); | ||
|
||
return new Response(newBody, response); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,21 @@ | ||
declare module '@it-astro:portal-gun' { | ||
type PortalAttrs = { | ||
to?: string; | ||
name?: string; | ||
children?: any; | ||
}; | ||
|
||
declare namespace JSX { | ||
interface IntrinsicElements { | ||
portal: PortalAttrs; | ||
} | ||
} | ||
|
||
import 'preact'; | ||
|
||
declare module 'preact' { | ||
export namespace JSX { | ||
export interface IntrinsicElements { | ||
portal: PortalAttrs; | ||
} | ||
} | ||
} |
Oops, something went wrong.