fix: better macro analysis
This commit is contained in:
@@ -146,7 +146,8 @@ export class ImportAnalysisEngine {
|
|||||||
private static readonly SHORTCODE_REGEX = /(?<!\[)\[(\w+)([^\]]*?)(?:\s*\/)?\](?!\])/g;
|
private static readonly SHORTCODE_REGEX = /(?<!\[)\[(\w+)([^\]]*?)(?:\s*\/)?\](?!\])/g;
|
||||||
|
|
||||||
// Regex to extract individual parameters from shortcode
|
// Regex to extract individual parameters from shortcode
|
||||||
private static readonly PARAM_REGEX = /(\w+)=["']([^"']*?)["']/g;
|
// Supports: key="value", key='value', and key=value (unquoted)
|
||||||
|
private static readonly PARAM_REGEX = /(\w+)=(?:"([^"]*)"|'([^']*)'|([^\s\]"']+))/g;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.turndown = new TurndownService({
|
this.turndown = new TurndownService({
|
||||||
@@ -574,6 +575,7 @@ export class ImportAnalysisEngine {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse parameters from a shortcode parameter string.
|
* Parse parameters from a shortcode parameter string.
|
||||||
|
* Supports: key="value", key='value', and key=value (unquoted)
|
||||||
*/
|
*/
|
||||||
private parseShortcodeParams(paramString: string): Record<string, string> {
|
private parseShortcodeParams(paramString: string): Record<string, string> {
|
||||||
const params: Record<string, string> = {};
|
const params: Record<string, string> = {};
|
||||||
@@ -583,7 +585,10 @@ export class ImportAnalysisEngine {
|
|||||||
|
|
||||||
let match;
|
let match;
|
||||||
while ((match = ImportAnalysisEngine.PARAM_REGEX.exec(paramString)) !== null) {
|
while ((match = ImportAnalysisEngine.PARAM_REGEX.exec(paramString)) !== null) {
|
||||||
params[match[1]] = match[2];
|
const key = match[1];
|
||||||
|
// Value is in group 2 (double-quoted), 3 (single-quoted), or 4 (unquoted)
|
||||||
|
const value = match[2] ?? match[3] ?? match[4] ?? '';
|
||||||
|
params[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return params;
|
return params;
|
||||||
|
|||||||
@@ -709,6 +709,42 @@ describe('ImportAnalysisEngine', () => {
|
|||||||
expect(youtubeMacro?.usages[0].params.id).toBe('singlequoted');
|
expect(youtubeMacro?.usages[0].params.id).toBe('singlequoted');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle shortcodes with unquoted parameters', async () => {
|
||||||
|
setupDbReturns([], [], []);
|
||||||
|
|
||||||
|
const wxrData = createWxrData({
|
||||||
|
posts: [createWxrPost({
|
||||||
|
content: '[youtube id=abc123def4g autoplay=true][gallery columns=4]',
|
||||||
|
})],
|
||||||
|
});
|
||||||
|
|
||||||
|
const report = await engine.analyzeWxr(wxrData, '/test.xml');
|
||||||
|
|
||||||
|
const youtubeMacro = report.macros.discovered.find(m => m.name === 'youtube');
|
||||||
|
expect(youtubeMacro?.usages[0].params.id).toBe('abc123def4g');
|
||||||
|
expect(youtubeMacro?.usages[0].params.autoplay).toBe('true');
|
||||||
|
|
||||||
|
const galleryMacro = report.macros.discovered.find(m => m.name === 'gallery');
|
||||||
|
expect(galleryMacro?.usages[0].params.columns).toBe('4');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should handle mixed quoted and unquoted parameters', async () => {
|
||||||
|
setupDbReturns([], [], []);
|
||||||
|
|
||||||
|
const wxrData = createWxrData({
|
||||||
|
posts: [createWxrPost({
|
||||||
|
content: '[video src="http://example.com/video.mp4" autoplay=true width=640]',
|
||||||
|
})],
|
||||||
|
});
|
||||||
|
|
||||||
|
const report = await engine.analyzeWxr(wxrData, '/test.xml');
|
||||||
|
|
||||||
|
const videoMacro = report.macros.discovered.find(m => m.name === 'video');
|
||||||
|
expect(videoMacro?.usages[0].params.src).toBe('http://example.com/video.mp4');
|
||||||
|
expect(videoMacro?.usages[0].params.autoplay).toBe('true');
|
||||||
|
expect(videoMacro?.usages[0].params.width).toBe('640');
|
||||||
|
});
|
||||||
|
|
||||||
it('should not detect our internal macro format as WordPress shortcodes', async () => {
|
it('should not detect our internal macro format as WordPress shortcodes', async () => {
|
||||||
setupDbReturns([], [], []);
|
setupDbReturns([], [], []);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user