Skip to content

Commit

Permalink
More testing coverage added
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalrymple committed Mar 11, 2024
1 parent f707285 commit a6a5813
Show file tree
Hide file tree
Showing 27 changed files with 900 additions and 786 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Converts the input htmlText to a valid latex string.
| `options.author` (`-a` or `--author`) | `string` | Yes | | If a document wrapper is added, the author will be set: `\author{Takashi Kovacs}`. |
| `options.includeDate` (`-d` or `--incude-date`) | `boolean` | Yes | `false` | If a document wrapper is added, the current date will be: `\date{\today}`. |
| `options.compilationDir` (`-cdr` or `--compilation-dir`) | `string` | Yes | `process.cwd` | If any images need to be downloaded for the latex compilation, they will be places in a 'images' subdirectory inside this directory. |
| `options.autoGenImageNames` (`-ain` or `--autogen-image-names`) | `boolean` | Yes | `true` | To avoid any weird file names, image files that are downloaded are automatically given a random Id with the extension of the original file. |
| `options.autoGenImageNames` (`-ain` or `--autogen-image-names`) | `boolean` | Yes | `false` | To avoid any weird file names, image files that are downloaded are automatically given a random Id with the extension of the original file. |
| `options.imageWidth` (`-iw` or `--image-width`) | `number` | Yes | | Allows you to set a image width. This would be in the form normally accepted by latex such as: `2cm`. |
| `options.imageHeight` (`-ih` or `--image-height`) | `number` | Yes | | Allows you to set a image height. This would be in the form normally accepted by latex such as: `2cm`. |
| `options.keepImageAspectRatio` (`-kar` or `--keep-aspect-ratio`) | `boolean` | Yes | `false` | Allows you to maintain the aspect ratio of the image. This also requires either the image width property or image height property to be set. |
Expand Down
24 changes: 13 additions & 11 deletions src/helpers/convert-element.mts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import type { ChildNode, ConvertElementOptions } from '../types.mts';
import type { ChildNode, ConvertElementOptions, ElementNode } from '../types.mts';
import { convertInlineElement } from './convert-inline-element.mts';
import * as Template from '../templates.mts';
import { convertUnorderedList } from './convert-unordered-list.mts';
import { convertOrderedLists } from './convert-ordered-list.mts';
import { convertOrderedList } from './convert-ordered-list.mts';
import { convertImage } from './convert-image.mts';
import { convertHeading } from './convert-heading.mts';
import { convertParagraph } from './convert-paragraph.mts';
import { convertTable } from './convert-table.mts';

export async function convertElement(
node: ChildNode,
options: ConvertElementOptions = {},
): Promise<string> {
options?: ConvertElementOptions,
): Promise<string | null> {
if (!node?.nodeName) return null;

switch (node.nodeName) {
case 'main':
case 'div':
case 'section':
case 'body':
Expand All @@ -28,12 +31,12 @@ export async function convertElement(
return nestedBlocks.filter(Boolean).join(Template.blockSeperator);
}

return '';
return null;
}
case 'ul':
return convertUnorderedList(node, options);
case 'ol':
return convertOrderedLists(node, options);
return convertOrderedList(node, options);
case 'img':
return convertImage(node, options);
case 'hr':
Expand All @@ -48,15 +51,14 @@ export async function convertElement(
case 'p':
return convertParagraph(node, options);
case 'table': {
if (node.childNodes.length === 0) break;
if (node.childNodes[0].nodeName === 'tbody') return convertTable(node.childNodes[0], options);
return convertTable(node, options);
if (node.childNodes.length === 0) return null;

// A tbody is always added even when missing from the original html
return convertTable(node?.childNodes[0] as ElementNode, options);
}
case 'code':
return Template.sourceCode(convertInlineElement(node, options).trim());
default:
return convertInlineElement(node, options);
}

return '';
}
9 changes: 6 additions & 3 deletions src/helpers/convert-image.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ export async function convertImage(
node: ElementNode,
{
compilationDir = process.cwd(),
autogenImageNames = true,
autogenImageNames = false,
debug = false,
imageWidth,
imageHeight,
keepImageAspectRatio,
centerImages,
} = {} as ConvertImageOptions,
): Promise<string> {
): Promise<string | null> {
const origPath = (node.attrs.find(({ name }) => name === 'src') as Attribute)?.value;

if (!origPath) return null;

const imagesDir = resolve(compilationDir, 'images');
const origPath = (node.attrs.find(({ name }) => name === 'src') as Attribute).value;
const ext = extname(origPath) || '.jpg';
const base = autogenImageNames ? `${generateId()}${ext}` : basename(origPath);
const localPath = resolve(imagesDir, base);
Expand Down
9 changes: 5 additions & 4 deletions src/helpers/convert-inline-element.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import type { Attribute, ChildNode, ConvertInlineElementOptions, TextNode } from

export function convertInlineElement(
node: ChildNode,
options: ConvertInlineElementOptions = {},
{ ignoreBreaks = true, ...options }: ConvertInlineElementOptions = {},
): string {
const opts = { ignoreBreaks, ...options };
// If this block has children, call fn on its child and obtain its value
const innerText =
'childNodes' in node && node.childNodes.length > 0
? node.childNodes.map((n) => convertInlineElement(n, options)).join('')
? node.childNodes.map((n) => convertInlineElement(n, opts)).join('')
: '';

switch (node.nodeName) {
case '#text':
return convertPlainText((node as unknown as TextNode).value, options);
return convertPlainText((node as unknown as TextNode).value, opts);
case 'b':
case 'strong':
return Template.bold(innerText);
Expand All @@ -30,7 +31,7 @@ export function convertInlineElement(
case 'sup':
return Template.superscript(innerText);
case 'br':
return options.ignoreBreaks ? ' ' : '\n\n';
return ignoreBreaks ? ' ' : '\n\n';
case 'a':
return Template.hyperlink(
innerText,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/convert-ordered-list.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { convertList } from './convert-list.mts';
import * as Template from '../templates.mts';
import type { ConvertOptions, ElementNode } from '../types.mts';

export async function convertOrderedLists(
export async function convertOrderedList(
node: ElementNode,
options: ConvertOptions = {},
): Promise<string> {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/convert-paragraph.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import type {

export function convertParagraph(
node: ElementNode,
options: ConvertParagraphOptions & ConvertInlineElementOptions = {},
options?: ConvertParagraphOptions & ConvertInlineElementOptions,
): string {
const convertedInlineText = convertInlineElement(node, options);
const trimmed = convertedInlineText.trim();

// Check if text is only an equation. If so, switch \( \) & $ $, for \[ \]
if (
!options.skipWrappingEquations &&
!options?.skipWrappingEquations &&
trimmed.match(/^(\$|\\\()/) &&
trimmed.match(/(\\\)|\$)$/)
) {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/convert-plain-text.mts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { decodeHTML } from 'entities';
import { ConvertInlineElementOptions } from '../types.mts';
import * as Template from '../templates.mts';

export function convertPlainText(
inputText: string,
{ ignoreBreaks = true, preferDollarInlineMath = false } = {},
{ ignoreBreaks = true, preferDollarInlineMath = false }: ConvertInlineElementOptions = {},
): string {
const breakReplacement = ignoreBreaks ? '' : '\n\n';
const cleanText = inputText
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/convert-text.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export async function convertText(
}: ConvertOptions = {},
): Promise<string> {
const root = parseFragment(htmlString);
const doc: (Promise<string> | string)[] = [];
const doc: (Promise<string> | string | null)[] = [];

if (includeDocumentWrapper) {
doc.push(Template.docClass(documentClass));

const packageImports =
includePackages.length > 0 ? includePackages : convertPackageImports(htmlString);

doc.push(Template.usePackages(packageImports));
if (packageImports.length > 0) doc.push(Template.usePackages(packageImports));

doc.push(Template.beginDocument({ title, includeDate, author }));
}
Expand All @@ -38,7 +38,7 @@ export async function convertText(
doc.push(...convertedElements.filter(Boolean));

// Add document wrapper if configuration is set
if (includeDocumentWrapper) doc.push(Template.endDocument);
if (includeDocumentWrapper) doc.push(Template.endDocument());

const converted = await Promise.all(doc);

Expand Down
1 change: 1 addition & 0 deletions src/helpers/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './convert-paragraph.mts';
export * from './convert-plain-text.mts';
export * from './convert-table-row.mts';
export * from './convert-table.mts';
export * from './convert-text.mts';
4 changes: 2 additions & 2 deletions src/index-bin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ program
'article',
)
.option(
'-ip --included-packages <included-packages...>',
'-ip --include-packages <include-packages...>',
'If a document wrapper is added, a list of used packages will be added via: `\\usepackage{packagename}`. If nothing is specified, the list of includes packages will be inferred from the html e.g cfrac => amsmath, img => graphicx, \therefore => amssymb',
)
.option(
Expand Down Expand Up @@ -87,7 +87,7 @@ program
skipWrappingEquations: options.skipWrappingEquations,
includeDocumentWrapper: options.includeDocumentWrapper,
documentClass: options.documentClass,
includedPackages: options.includedPackages,
includePackages: options.includePackages,
title: options.title,
author: options.author,
includeDate: options.includeDate,
Expand Down
6 changes: 3 additions & 3 deletions src/templates.mts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function image(
}

export function usePackages(packageNames: string[]): string {
return newLineSuffix(packageNames.map((n) => `\\usepackage{${n}}`).join('\n'));
return packageNames.map((n) => `\\usepackage{${n}}`).join('\n');
}

export function beginDocument({
Expand All @@ -90,10 +90,10 @@ export function beginDocument({

if (title) beginningText.push(newLinePrefix('\\maketitle'));

return beginningText.join('\n');
return newLineSuffix(beginningText.join('\n'));
}

export const endDocument = newLinePrefix('\\end{document}');
export const endDocument = () => newLinePrefix('\\end{document}');

export const docClass = (className: string): string => `\\documentclass{${className}}`;

Expand Down
4 changes: 2 additions & 2 deletions src/types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export type ConvertImageOptions = {
centerImages?: boolean;
};

export interface ConvertParagraphOptions {
export type ConvertParagraphOptions = {
skipWrappingEquations?: boolean;
}
};

export type ConvertElementOptions = ConvertInlineElementOptions &
ConvertImageOptions &
Expand Down
70 changes: 0 additions & 70 deletions test/integration/convertText.mts

This file was deleted.

Loading

0 comments on commit a6a5813

Please sign in to comment.