This commit is contained in:
Morten Olsen
2022-03-30 16:34:56 +02:00
committed by Morten Olsen
parent b654ba7e74
commit e6e938bd42
44 changed files with 386 additions and 263 deletions

View File

@@ -10,9 +10,9 @@ const withLatex = (nextConfig = {}) => {
outputPath = "../../";
}
config.module.rules.push({
test: /\.png.yml$/,
test: /\.gen.yml$/,
use: [{
loader: 'file-loader',
loader: require.resolve('./webpack.js'),
options: {
publicPath: `${nextConfig.assetPrefix || nextConfig.basePath || ''}/_next/static/images/`,
@@ -27,8 +27,6 @@ const withLatex = (nextConfig = {}) => {
},
esModule: nextConfig.esModule || false,
},
}, {
loader: require.resolve('./webpack.js'),
}],
});
if (typeof nextConfig.webpack === "function") {

View File

@@ -0,0 +1,31 @@
require('reflect-metadata');
require('@babel/register')({
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
});
const loaderUtils = require('loader-utils');
const path = require('path');
const yaml = require('yaml');
const { generate } = require('../../../src/generators');
module.exports = function (source) {
var callback = this.async();
const location = this.resourcePath;
const definition = yaml.parse(source);
const options = this.getOptions();
generate(definition, location)
.then((output) => {
const files = Object.entries(output).reduce((output, [key, value]) => {
const { name, content } = value;
const targetName = loaderUtils.interpolateName(this, `[contenthash]/${name}`, {content: content});
const location = path.join(options.outputPath, targetName);
const publicPath = path.join(options.publicPath, targetName);
this.emitFile(location, content);
return {
...output,
[key]: publicPath,
}
}, {});
callback(null, `module.exports = ${JSON.stringify(files)}`);
})
.catch(callback);
}

View File

@@ -1,16 +0,0 @@
require('reflect-metadata');
require('@babel/register')({
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
});
const { generateImage } = require('../../../src/images');
module.exports = function (source) {
var callback = this.async();
const location = this.resourcePath;
generateImage(source, location)
.then((canvas) => {
const buffer = canvas.toBuffer('image/png', {})
callback(null, buffer);
})
.catch(callback);
}

View File

@@ -1,34 +0,0 @@
const withLatex = (nextConfig = {}) => {
return Object.assign({}, nextConfig, {
webpack(config, options) {
const {isServer, dev} = options;
let outputPath = '';
if (isServer && dev) {
outputPath = "../";
} else if (isServer) {
outputPath = "../../";
}
config.module.rules.push({
test: /\.tex.yml$/,
use: [{
loader: 'file-loader',
options: {
publicPath: `${nextConfig.assetPrefix || nextConfig.basePath || ''}/_next/static/images/`,
outputPath: `${outputPath}static/images/`,
name: "[name]-[hash].pdf",
esModule: nextConfig.esModule || false,
},
}, {
loader: require.resolve('./webpack.js'),
}],
});
if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options);
}
return config;
},
});
}
module.exports = withLatex;

View File

@@ -1,31 +0,0 @@
require('reflect-metadata');
require('@babel/register')({
extensions: [".es6", ".es", ".jsx", ".js", ".mjs", ".ts"],
});
const latex = require("node-latex")
var Readable = require('stream').Readable
const { generateLatex } = require('../../../src/latex');
module.exports = function (source) {
var callback = this.async();
const location = this.resourcePath;
generateLatex(source, location)
.then((result) => {
const chunks = [];
const input = new Readable();
input.push(result);
input.push(null);
const latexStream = latex(input);
latexStream.on('data', (chunk) => {
chunks.push(Buffer.from(chunk));
})
latexStream.on('finish', () => {
const result = Buffer.concat(chunks);
callback(null, result);
})
latexStream.on('error', (err) => {
callback(err);
})
})
.catch(callback);
}