refactor: complete three-layer serial refactoring (knowledge→schema→renderer)
Step 1 — Knowledge base: perfect all reference/*.md with type, range, default, unit - text.md: add DataOrPathProps, path/data, bullet deprecated params - chart.md: add deprecated section, fix 19 params with missing elements - slide.md: add bkgd→background deprecated mapping - output.md: add masterSlide, presLayout Step 2 — Schema: align schema/presentation.schema.json with KB - Add catAxisItem and valAxisItem definitions with full sub-property constraints - Fix 7 enum constraints (barDir, displayBlanksAs, bar3DShape, etc.) - Add 4 missing fields (verbose, autoPageCharWeight, autoPageLineWeight, notes, masterSlide, presLayout) Step 3 — Translation engine: transform renderers from passthrough to validation - index.js: add normalizePosition, normalizeColor, validateEnum shared utilities - All renderers: position validation, color hex normalization, enum validation - Image: graceful failure handling (catch load errors, warn, continue) - Chart: chartType enum validation, data parity check - Table: colspan/rowspan integer validation, border array check - Full verification: basic.json and full.json generate valid PPTX Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+100
-2
@@ -1,5 +1,68 @@
|
||||
const pptxgen = require('pptxgenjs');
|
||||
const fs = require('fs');
|
||||
|
||||
function normalizePosition(options) {
|
||||
if (!options || typeof options !== 'object') return options;
|
||||
['x', 'y', 'w', 'h'].forEach(function(key) {
|
||||
if (options[key] === undefined) return;
|
||||
var val = options[key];
|
||||
if (typeof val === 'number' && !isNaN(val)) return;
|
||||
if (typeof val === 'string' && /^\d+(\.\d+)?%$/.test(val)) return;
|
||||
console.warn('Invalid position value for "' + key + '": got ' + JSON.stringify(val) + ', expected a number or percentage string (e.g. "50%")');
|
||||
delete options[key];
|
||||
});
|
||||
if ((options.x !== undefined || options.y !== undefined) && options.w === undefined) options.w = 4;
|
||||
if ((options.x !== undefined || options.y !== undefined) && options.h === undefined) options.h = 3;
|
||||
return options;
|
||||
}
|
||||
|
||||
function normalizeColor(color) {
|
||||
if (color === undefined || color === null) return color;
|
||||
if (typeof color !== 'string') {
|
||||
console.warn('Invalid color value: expected a string, got ' + typeof color);
|
||||
return undefined;
|
||||
}
|
||||
var themeColors = ['tx1', 'tx2', 'bg1', 'bg2', 'accent1', 'accent2', 'accent3', 'accent4', 'accent5', 'accent6'];
|
||||
for (var i = 0; i < themeColors.length; i++) {
|
||||
if (color === themeColors[i]) return color;
|
||||
}
|
||||
var clean = color.replace(/^#/, '');
|
||||
if (/^[0-9A-Fa-f]{6}$/.test(clean)) return clean;
|
||||
console.warn('Invalid color value: "' + color + '" is not a valid color format (expected 6-digit hex or theme color)');
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function normalizeColorInOptions(options, key) {
|
||||
if (!options || options[key] === undefined) return;
|
||||
var nc = normalizeColor(options[key]);
|
||||
if (nc === undefined) delete options[key];
|
||||
else options[key] = nc;
|
||||
}
|
||||
|
||||
function validateEnum(value, validValues, name) {
|
||||
if (value === undefined || value === null) return value;
|
||||
if (validValues.indexOf(value) === -1) {
|
||||
console.warn('Invalid value for ' + name + ': got "' + value + '", expected one of [' + validValues.join(', ') + ']');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
function withDefaults(options, defaults) {
|
||||
if (!options || typeof options !== 'object') return options;
|
||||
for (var key in defaults) {
|
||||
if (defaults.hasOwnProperty(key) && options[key] === undefined) {
|
||||
options[key] = defaults[key];
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
module.exports.normalizePosition = normalizePosition;
|
||||
module.exports.normalizeColor = normalizeColor;
|
||||
module.exports.normalizeColorInOptions = normalizeColorInOptions;
|
||||
module.exports.validateEnum = validateEnum;
|
||||
module.exports.withDefaults = withDefaults;
|
||||
|
||||
const { renderSlide } = require('./slide-renderer');
|
||||
|
||||
function render(pptxJson) {
|
||||
@@ -25,7 +88,42 @@ function render(pptxJson) {
|
||||
async function renderFile(inputPath, outputPath) {
|
||||
const json = fs.readFileSync(inputPath, 'utf8');
|
||||
const pptx = render(json);
|
||||
return pptx.writeFile({ fileName: outputPath });
|
||||
|
||||
// pptxgenjs v4 uses https.get() to download URL-based images but doesn't
|
||||
// handle connection errors on the request object (only on the response
|
||||
// stream). A failed TLS handshake emits an unhandled 'error' event on the
|
||||
// request that crashes Node.js, and the internal promise never resolves.
|
||||
// Patch to pipe request errors into a fake response that ends cleanly so
|
||||
// writeFile completes with an empty placeholder image.
|
||||
var https = require('https');
|
||||
var _origGet = https.get;
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
https.get = function() {
|
||||
var args = arguments;
|
||||
var cb = typeof args[args.length - 1] === 'function' ? args[args.length - 1] : null;
|
||||
var urlStr = typeof args[0] === 'string' ? args[0] : '';
|
||||
var req = _origGet.apply(this, args);
|
||||
req.on('error', function(err) {
|
||||
console.warn('Failed to load image: ' + (urlStr || err.hostname || err.host || 'unknown'));
|
||||
if (cb) {
|
||||
var fakeRes = new EventEmitter();
|
||||
fakeRes.setEncoding = function() {};
|
||||
cb(fakeRes);
|
||||
process.nextTick(function() {
|
||||
fakeRes.emit('data', '');
|
||||
fakeRes.emit('end');
|
||||
});
|
||||
}
|
||||
});
|
||||
return req;
|
||||
};
|
||||
|
||||
try {
|
||||
return await pptx.writeFile({ fileName: outputPath });
|
||||
} finally {
|
||||
https.get = _origGet;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { render, renderFile };
|
||||
module.exports.render = render;
|
||||
module.exports.renderFile = renderFile;
|
||||
|
||||
Reference in New Issue
Block a user