Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tokenizer extension position #3594

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 13 additions & 10 deletions src/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { _Renderer } from './Renderer.ts';
import { _Tokenizer } from './Tokenizer.ts';
import { _TextRenderer } from './TextRenderer.ts';
import { escape } from './helpers.ts';
import type { MarkedExtension, MarkedOptions } from './MarkedOptions.ts';
import { tokenizerBlockPositions, tokenizerInlinePositions, type MarkedExtension, type MarkedOptions } from './MarkedOptions.ts';
import type { Token, Tokens, TokensList } from './Tokens.ts';

export type MaybePromise = void | Promise<void>;
Expand Down Expand Up @@ -74,7 +74,7 @@ export class Marked {
}

use(...args: MarkedExtension[]) {
const extensions: MarkedOptions['extensions'] = this.defaults.extensions || { renderers: {}, childTokens: {} };
const extensions: MarkedOptions['extensions'] = this.defaults.extensions || { renderers: {}, childTokens: {}, tokenizers: {} };

args.forEach((pack) => {
// copy options to new object
Expand Down Expand Up @@ -105,23 +105,26 @@ export class Marked {
}
}
if ('tokenizer' in ext) { // Tokenizer Extensions
if (!ext.level || (ext.level !== 'block' && ext.level !== 'inline')) {
if (ext.level && (ext.level !== 'block' && ext.level !== 'inline')) {
throw new Error("extension level must be 'block' or 'inline'");
}
const extLevel = extensions[ext.level];
if (extLevel) {
extLevel.unshift(ext.tokenizer);
} else {
extensions[ext.level] = [ext.tokenizer];
if (ext.position && ![...tokenizerBlockPositions, ...tokenizerInlinePositions].includes(ext.position)) {
throw new Error(`extension position must be one of '${tokenizerBlockPositions.join("', '")}', '${tokenizerInlinePositions.join("', '")}'`);
}
if (!ext.level && !ext.position) {
throw new Error('extension level or position is required');
}
const position = ext.position || (ext.level === 'block' ? tokenizerBlockPositions[0] : tokenizerInlinePositions[0]);
extensions.tokenizers[position] = extensions.tokenizers[position] || [];
extensions.tokenizers[position].unshift(ext.tokenizer);
if (ext.start) { // Function to check for start of token
if (ext.level === 'block') {
if (tokenizerBlockPositions.includes(position as typeof tokenizerBlockPositions[number])) {
if (extensions.startBlock) {
extensions.startBlock.push(ext.start);
} else {
extensions.startBlock = [ext.start];
}
} else if (ext.level === 'inline') {
} else if (tokenizerInlinePositions.includes(position as typeof tokenizerInlinePositions[number])) {
if (extensions.startInline) {
extensions.startInline.push(ext.start);
} else {
Expand Down
Loading
Loading