Skip to content

Commit 789fa06

Browse files
feat(vite-plugin-angular): add pipeline to transform analog markdown template (#875)
1 parent f72cabb commit 789fa06

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { angular } from './lib/angular-vite-plugin';
22
export { PluginOptions } from './lib/angular-vite-plugin';
33
export { compileAnalogFile } from './lib/authoring/analog';
4+
export {
5+
MarkdownTemplateTransform,
6+
defaultMarkdownTemplateTransforms,
7+
} from './lib/authoring/markdown-transform';
48

59
export default angular;

packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {
2525
SourceFileCache,
2626
} from './utils/devkit';
2727
import { getFrontmatterMetadata } from './authoring/frontmatter';
28+
import {
29+
defaultMarkdownTemplateTransforms,
30+
MarkdownTemplateTransform,
31+
} from './authoring/markdown-transform';
2832

2933
export interface PluginOptions {
3034
tsconfig?: string;
@@ -46,6 +50,7 @@ export interface PluginOptions {
4650
| {
4751
include: string[];
4852
};
53+
markdownTemplateTransforms?: MarkdownTemplateTransform[];
4954
};
5055
supportedBrowsers?: string[];
5156
transformFilter?: (code: string, id: string) => boolean;
@@ -92,6 +97,9 @@ export function angular(options?: PluginOptions): Plugin[] {
9297
supportedBrowsers: options?.supportedBrowsers ?? ['safari 15'],
9398
jit: options?.jit,
9499
supportAnalogFormat: options?.experimental?.supportAnalogFormat ?? false,
100+
markdownTemplateTransforms:
101+
options?.experimental?.markdownTemplateTransforms ??
102+
defaultMarkdownTemplateTransforms,
95103
};
96104

97105
// The file emitter created during `onStart` that will be used during the build in `onLoad` callbacks for TS files
@@ -502,6 +510,7 @@ export function angular(options?: PluginOptions): Plugin[] {
502510
inlineStylesExtension: pluginOptions.inlineStylesExtension,
503511
supportAnalogFormat: pluginOptions.supportAnalogFormat,
504512
isProd,
513+
markdownTemplateTransforms: pluginOptions.markdownTemplateTransforms,
505514
});
506515
}
507516
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export type MarkdownTemplateTransform = (
2+
content: string,
3+
fileName: string
4+
) => string | Promise<string>;
5+
6+
export const defaultMarkdownTemplateTransform: MarkdownTemplateTransform =
7+
async (content: string) => {
8+
const { MarkedSetupService } = await import('./marked-setup.service');
9+
10+
// read template sections, parse markdown
11+
const markedSetupService = new MarkedSetupService();
12+
const mdContent = markedSetupService
13+
.getMarkedInstance()
14+
.parse(content) as unknown as Promise<string>;
15+
16+
return mdContent;
17+
};
18+
19+
export const defaultMarkdownTemplateTransforms: MarkdownTemplateTransform[] = [
20+
defaultMarkdownTemplateTransform,
21+
];

packages/vite-plugin-angular/src/lib/host.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { readFileSync } from 'node:fs';
44
import * as ts from 'typescript';
55
import { compileAnalogFile } from './authoring/analog';
66
import { TEMPLATE_TAG_REGEX } from './authoring/constants';
7+
import { MarkdownTemplateTransform } from './authoring/markdown-transform';
78

89
export function augmentHostWithResources(
910
host: ts.CompilerHost,
@@ -21,6 +22,7 @@ export function augmentHostWithResources(
2122
};
2223

2324
isProd?: boolean;
25+
markdownTemplateTransforms?: MarkdownTemplateTransform[];
2426
} = {}
2527
) {
2628
const resourceHost = host as CompilerHost;
@@ -97,22 +99,16 @@ export function augmentHostWithResources(
9799
resourceHost.readResource = async function (fileName: string) {
98100
const filePath = normalizePath(fileName);
99101

100-
const content = (this as any).readFile(filePath);
102+
let content = (this as any).readFile(filePath);
103+
101104
if (content === undefined) {
102105
throw new Error('Unable to locate component resource: ' + fileName);
103106
}
104107

105108
if (fileName.includes('virtual-analog:')) {
106-
const { MarkedSetupService } = await import(
107-
'./authoring/marked-setup.service'
108-
);
109-
// read template sections, parse markdown
110-
const markedSetupService = new MarkedSetupService();
111-
const mdContent = markedSetupService
112-
.getMarkedInstance()
113-
.parse(content) as unknown as Promise<string>;
114-
115-
return mdContent;
109+
for (const transform of options.markdownTemplateTransforms || []) {
110+
content = await transform(content, fileName);
111+
}
116112
}
117113

118114
return content;

0 commit comments

Comments
 (0)