Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const content = seeMarkReactParse(markdown);

| Option Name | Type | Default Value | Description |
| ------------------------- | ------- | -------------- | ------------------------------------------------------------------ |
| includeMathExtensions | boolean | true | Include math (LaTeX/AsciiMath) and Nemeth extensions. |
| latexDelimiter | string | 'bracket' | The delimiter for LaTeX expressions. Options: 'bracket', 'dollar'. |
| documentFormat | string | 'inline' | The format of the document. Options: 'inline', 'block'. |
| imageFiles | object | { [ID]: File } | A map of image IDs to File objects for image rendering. |
Expand Down Expand Up @@ -123,6 +124,13 @@ const toc = createTableOfContents(markdown);

`createTableOfContents` parses a markdown string and returns a flat array of all h1–h6 headings in document order. The `id` of each entry is generated with the same slugify logic used by the seemark's markdown parser, so IDs are guaranteed to match the `id` prop on rendered heading components.

### Options

| Option Name | Type | Default Value | Description |
| --------------------- | ------- | ------------- | -------------------------------------------------------------------------------------------------------- |
| includeMathExtensions | boolean | true | Include math (LaTeX/AsciiMath) and Nemeth extensions. Must match the value used by the renderer. |
| latexDelimiter | string | 'bracket' | The delimiter for LaTeX expressions. Must match the value used by the renderer. Options: 'bracket', 'dollar'. |

### Return value

Each entry in the returned array has the following shape:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@coseeing/see-mark",
"version": "1.8.0",
"version": "1.9.0",
"description": "A markdown parser for a11y",
"main": "./lib/see-mark.cjs",
"files": [
Expand Down
4 changes: 1 addition & 3 deletions src/markdown-processor/markdown-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ import iframe from './marked-extentions/iframe';

export const createMarkdownProcessor = (options = {}) => {
const asciimathDelimiter = 'graveaccent';

return markedProcessorFactory({
latexDelimiter: options.latexDelimiter,
asciimathDelimiter,
documentFormat: options.documentFormat,
imageFiles: options.imageFiles,
shouldBuildImageObjectURL: options.shouldBuildImageObjectURL,
extensions: [
math,
nemeth,
...(options.includeMathExtensions !== false ? [math, nemeth] : []),
alert,
heading,
internalLink,
Expand Down
36 changes: 36 additions & 0 deletions src/markdown-processor/markdown-processor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,42 @@ describe('markdownProcessor', () => {
expect(container).toMatchSnapshot();
});

it('should not render math when includeMathExtensions is false', () => {
const markdownContent = '\\({{\\left( -3 \\right)}^{3}}\\)';
const options = {
latexDelimiter: 'bracket',
documentFormat: 'inline',
imageFiles: {},
includeMathExtensions: false,
};

const result = markdownProcessor(markdownContent, options);

const container = createDOMFromHTML(result);

const mathEl = getElementByType(container, SUPPORTED_COMPONENT_TYPES.MATH);

expect(mathEl).toBeNull();
});

it('should not render nemeth when includeMathExtensions is false', () => {
const markdownContent = '@⠁⠘⠆@';
const options = {
latexDelimiter: 'bracket',
documentFormat: 'inline',
imageFiles: {},
includeMathExtensions: false,
};

const result = markdownProcessor(markdownContent, options);

const container = createDOMFromHTML(result);

const mathEl = getElementByType(container, SUPPORTED_COMPONENT_TYPES.MATH);

expect(mathEl).toBeNull();
});

it('should process alert', () => {
const markdownContent = `> [!WARNING]\n> Critical content demanding immediate user attention due to potential risks.`;
const options = {
Expand Down
1 change: 1 addition & 0 deletions src/parsers/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const DEFAULT_OPTINOS = {
documentFormat: 'inline',
imageFiles: null,
shouldBuildImageObjectURL: false,
includeMathExtensions: true,
};

export const createMarkdownParserOptions = (options = DEFAULT_OPTINOS) => {
Expand Down
2 changes: 2 additions & 0 deletions src/table-of-contents/create-table-of-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function extractPlainText(inlineTokens = []) {
* Must match the value used by the markdown renderer so that math tokens
* inside headings are recognised and their text is correctly extracted.
* Defaults to 'bracket'.
* @param {boolean} [options.includeMathExtensions=true] - Whether to enable math extensions. Defaults to true.
* @returns {{ level: number, id: string, text: string }[]}
*
* @example
Expand All @@ -46,6 +47,7 @@ function extractPlainText(inlineTokens = []) {
const createTableOfContents = (markdown, options = {}) => {
const { lexer } = createMarkdownProcessor({
latexDelimiter: options.latexDelimiter ?? 'bracket',
includeMathExtensions: options.includeMathExtensions ?? true,
});
const tokens = lexer(markdown);
const usedIds = new Map();
Expand Down
Loading