Skip to content

Commit

Permalink
Merge pull request #3 from cm-ayf/options
Browse files Browse the repository at this point in the history
Options
  • Loading branch information
cm-ayf authored Jan 20, 2024
2 parents 011902a + 2aa9a10 commit eb43844
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 354 deletions.
13 changes: 11 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@ declare module 'unified' {
}
}

export default function remarkAttributeList(this: Processor) {
export interface Options {
allowNoSpaceBeforeName?: boolean;
allowUnderscoreInId?: boolean;
}

export default function remarkAttributeList(
this: Processor,
options?: Options,
) {
const data = this.data();

data.micromarkExtensions ??= [];
data.micromarkExtensions.push(micromarkExtension);
data.micromarkExtensions.push(micromarkExtension(options));

data.fromMarkdownExtensions ??= [];
data.fromMarkdownExtensions.push(fromMarkdownExtension);
Expand Down
68 changes: 36 additions & 32 deletions lib/micromark/block-inline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
State,
TokenizeContext,
} from 'micromark-util-types';
import type {Options} from '../index.js';
import {attributeList} from './list.js';

declare module 'micromark-util-types' {
Expand All @@ -24,45 +25,48 @@ declare module 'micromark-util-types' {
}
}

export const blockInlineAttributeList: Construct = {
tokenize,
};

function tokenize(
this: TokenizeContext,
effects: Effects,
ok: State,
nok: State,
): State {
const start: State = (code) => {
if (code !== codes.leftCurlyBrace) return nok(code);
effects.enter('blockInlineAttributeList');
effects.enter('blockInlineAttributeListMarker');
effects.consume(code);
effects.exit('blockInlineAttributeListMarker');
return colon;
export function blockInlineAttributeList(options?: Options): Construct {
const list = attributeList(options);
return {
tokenize,
};

const colon: State = (code) => {
if (code === codes.colon) {
function tokenize(
this: TokenizeContext,
effects: Effects,
ok: State,
nok: State,
): State {
const start: State = (code) => {
if (code !== codes.leftCurlyBrace) return nok(code);
effects.enter('blockInlineAttributeList');
effects.enter('blockInlineAttributeListMarker');
effects.consume(code);
effects.exit('blockInlineAttributeListMarker');
return colon;
};

return effects.attempt(attributeList, end, nok);
}
const colon: State = (code) => {
if (code === codes.colon) {
effects.enter('blockInlineAttributeListMarker');
effects.consume(code);
effects.exit('blockInlineAttributeListMarker');

return nok(code);
};
return effects.attempt(list, end, nok);
}

const end: State = (code) => {
if (code !== codes.rightCurlyBrace) return nok(code);
effects.enter('blockInlineAttributeListMarker');
effects.consume(code);
effects.exit('blockInlineAttributeListMarker');
effects.exit('blockInlineAttributeList');
return ok;
};
return nok(code);
};

const end: State = (code) => {
if (code !== codes.rightCurlyBrace) return nok(code);
effects.enter('blockInlineAttributeListMarker');
effects.consume(code);
effects.exit('blockInlineAttributeListMarker');
effects.exit('blockInlineAttributeList');
return ok;
};

return start;
return start;
}
}
116 changes: 60 additions & 56 deletions lib/micromark/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
} from 'micromark-util-types';
import {codes} from 'micromark-util-symbol';
import {ok as assert} from 'devlop';
import type {Options} from '../index.js';
import {referenceNameIsh} from './reference-name-ish.js';
import {attributeList} from './list.js';

Expand Down Expand Up @@ -40,71 +41,74 @@ declare module 'micromark-util-types' {
}
}

export const attributeListDefinition: Construct = {
tokenize,
resolve,
};

function tokenize(
this: TokenizeContext,
effects: Effects,
ok: State,
nok: State,
): State {
const start: State = (code) => {
if (code !== codes.leftCurlyBrace) return nok(code);
effects.enter('attributeListDefinition');
effects.enter('attributeListDefinitionMarker');
effects.consume(code);
effects.exit('attributeListDefinitionMarker');
return referenceStart;
export function attributeListDefinition(options?: Options): Construct {
const list = attributeList(options);
return {
tokenize,
resolve,
};

const referenceStart: State = (code) => {
if (code === codes.colon) {
effects.enter('attributeListDefinitionReference');
effects.enter('attributeListDefinitionReferenceMarker');
function tokenize(
this: TokenizeContext,
effects: Effects,
ok: State,
nok: State,
): State {
const start: State = (code) => {
if (code !== codes.leftCurlyBrace) return nok(code);
effects.enter('attributeListDefinition');
effects.enter('attributeListDefinitionMarker');
effects.consume(code);
effects.exit('attributeListDefinitionReferenceMarker');
return effects.attempt(referenceNameIsh, referenceEnd, nok);
}
effects.exit('attributeListDefinitionMarker');
return referenceStart;
};

return nok(code);
};
const referenceStart: State = (code) => {
if (code === codes.colon) {
effects.enter('attributeListDefinitionReference');
effects.enter('attributeListDefinitionReferenceMarker');
effects.consume(code);
effects.exit('attributeListDefinitionReferenceMarker');
return effects.attempt(referenceNameIsh, referenceEnd, nok);
}

const referenceEnd: State = (code) => {
if (code !== codes.colon) return nok(code);
effects.enter('attributeListDefinitionReferenceMarker');
effects.consume(code);
effects.exit('attributeListDefinitionReferenceMarker');
effects.exit('attributeListDefinitionReference');
return nok(code);
};

return effects.attempt(attributeList, end, nok);
};
const referenceEnd: State = (code) => {
if (code !== codes.colon) return nok(code);
effects.enter('attributeListDefinitionReferenceMarker');
effects.consume(code);
effects.exit('attributeListDefinitionReferenceMarker');
effects.exit('attributeListDefinitionReference');

const end: State = (code) => {
if (code !== codes.rightCurlyBrace) return nok(code);
effects.enter('attributeListDefinitionMarker');
effects.consume(code);
effects.exit('attributeListDefinitionMarker');
effects.exit('attributeListDefinition');
return ok;
};
return effects.attempt(list, end, nok);
};

return start;
}
const end: State = (code) => {
if (code !== codes.rightCurlyBrace) return nok(code);
effects.enter('attributeListDefinitionMarker');
effects.consume(code);
effects.exit('attributeListDefinitionMarker');
effects.exit('attributeListDefinition');
return ok;
};

function resolve(events: Event[]): Event[] {
const exitReferenceMarkerStart = events.findIndex(
([type, token]) =>
type === 'exit' &&
token.type === 'attributeListDefinitionReferenceMarker',
);
return start;
}

function resolve(events: Event[]): Event[] {
const exitReferenceMarkerStart = events.findIndex(
([type, token]) =>
type === 'exit' &&
token.type === 'attributeListDefinitionReferenceMarker',
);

const enterReferenceNameIsh = events[exitReferenceMarkerStart + 1];
assert(enterReferenceNameIsh?.[0] === 'enter');
assert(enterReferenceNameIsh?.[1].type === 'referenceNameIsh');
enterReferenceNameIsh[1].type = 'attributeListDefinitionReferenceName';
const enterReferenceNameIsh = events[exitReferenceMarkerStart + 1];
assert(enterReferenceNameIsh?.[0] === 'enter');
assert(enterReferenceNameIsh?.[1].type === 'referenceNameIsh');
enterReferenceNameIsh[1].type = 'attributeListDefinitionReferenceName';

return events;
return events;
}
}
25 changes: 14 additions & 11 deletions lib/micromark/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type {Extension as MicromarkExtension} from 'micromark-util-types';
import {codes} from 'micromark-util-symbol';
import type {Options} from '../index.js';
import {attributeListDefinition} from './definition.js';
import {blockInlineAttributeList} from './block-inline.js';
import {spanInlineAttributeList} from './span-inline.js';

export const micromarkExtension: MicromarkExtension = {
contentInitial: {
[codes.leftCurlyBrace]: attributeListDefinition,
},
flow: {
[codes.leftCurlyBrace]: blockInlineAttributeList,
},
text: {
[codes.leftCurlyBrace]: spanInlineAttributeList,
},
};
export function micromarkExtension(options?: Options): MicromarkExtension {
return {
contentInitial: {
[codes.leftCurlyBrace]: attributeListDefinition(options),
},
flow: {
[codes.leftCurlyBrace]: blockInlineAttributeList(options),
},
text: {
[codes.leftCurlyBrace]: spanInlineAttributeList(options),
},
};
}
Loading

0 comments on commit eb43844

Please sign in to comment.