@mintlify/prebuild
Version:
Helpful functions for Mintlify's prebuild step
46 lines (45 loc) • 1.76 kB
JavaScript
import { isAbsolute, sep, relative } from 'path';
const getPosition = (error) => {
const err = error;
if (typeof err.line === 'number') {
return { line: err.line, column: err.column };
}
const pos = err.position;
const start = pos?.start;
if (typeof start?.line === 'number') {
return start;
}
// Parse position from string manually because mdast-util-mdx-jsx has a bug where they attempt to use the
// closing tag position in the error handler for when there is no closing tag
// https://github.com/syntax-tree/mdast-util-mdx-jsx/blob/998d98d0aa29fe9ed12c636ddf1ed39f9a018096/lib/index.js#L454
const reason = typeof err.reason === 'string' ? err.reason : '';
const lastParen = reason.lastIndexOf('(');
if (lastParen !== -1 && reason.endsWith(')')) {
const inside = reason.slice(lastParen + 1, -1);
const firstPart = inside.split('-')[0] || '';
const [lineStr, colStr] = firstPart.split(':');
const line = Number(lineStr);
if (!isNaN(line) && line > 0) {
return { line, column: Number(colStr) || undefined };
}
}
return null;
};
export const getLocationErrString = (filePath, contentDirectoryPath, error) => {
if (isAbsolute(filePath)) {
filePath = relative(contentDirectoryPath, filePath);
}
if (!filePath.startsWith(`.${sep}`) && !isAbsolute(filePath)) {
filePath = `.${sep}${filePath}`;
}
if (error && typeof error === 'object') {
const pos = getPosition(error);
if (pos?.line != null) {
filePath += `:${pos.line}`;
if (pos.column != null) {
filePath += `:${pos.column}`;
}
}
}
return filePath;
};