Skip to content

Commit

Permalink
feat(parser): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Apr 17, 2024
1 parent a56a3bf commit d0ee36f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microflash/fenceparser",
"version": "3.0.0-beta.3",
"version": "3.0.0-beta.4",
"description": "A metadata parser for code fences in markdown",
"main": "src/index.js",
"scripts": {
Expand Down
61 changes: 28 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,58 +28,53 @@ export default class FenceParser {
this.options = defu(options, FenceParser.DefaultOptions);
}

stripParantheses(token) {
const startIndex = token.indexOf("{");
const stopIndex = token.indexOf("}");
const key = token.substring(0, startIndex) || this.options.rangeKey;
const value = token.substring(startIndex + 1, stopIndex);
return {
key,
value
extractRawPair(token, label) {
switch (label) {
case "range":
const startIndex = token.indexOf("{");
const stopIndex = token.indexOf("}");
return {
key: token.substring(0, startIndex) || this.options.rangeKey,
value: token.substring(startIndex + 1, stopIndex)
};
case "pair":
const [, key = "key", v1, v2, v3] = token.match(keyValuePairPattern);
return { key, value: v1 || v2 || v3 };
default:
console.warn(`Unsupported token type ${label}`);
}
}

process(token0) {
extractPair(token0) {
const label = token0.tokenType.LABEL;
const token = token0.image;
const { key, value } = this.extractRawPair(token, label);
const type = token0.tokenType.name;
switch(type) {
case "Range0":
const r0 = this.stripParantheses(token);
return {
key: r0.key,
value: [Number(r0.value)],
type: label
};
return { key, value: [Number(value)], label };
case "Range2":
const r2 = this.stripParantheses(token);
const [ b0, b1 ] = r2.value.split("..").map((bound) => Number(bound));
const [ b0, b1 ] = value.split("..").map((bound) => Number(bound));
const start = Math.min(b0, b1);
const stop = Math.max(b0, b1);
const size = stop - start + 1;
return {
key: r2.key,
key,
value: Array.from({ length: size }, (_, i) => i + start),
type: label
label
};
case "Ranges":
const rx = this.stripParantheses(token);
return {
key: rx.key,
value: rx.value.split(",")
.map(res => "{" + res.trim() + "}")
key,
value: value.split(",")
.map(res => `{${res.trim()}}`)
.flatMap(res => FenceParser.FenceLexer.tokenize(res).tokens)
.map(res => this.process(res))
.map(res => this.extractPair(res))
.flatMap(res => res.value),
type: label
label
}
case "KeyValuePair":
let [, key = "key", v1, v2, v3] = token.match(keyValuePairPattern);
return {
key,
value: v1 || v2 || v3,
type: label
};
return { key, value, label };
default:
console.warn(`Unsupported token type ${type}`);
}
Expand All @@ -90,8 +85,8 @@ export default class FenceParser {

const response = {};
for (const token of tokens) {
const { key, value, type } = this.process(token);
const isRange = type === "range";
const { key, value, label } = this.extractPair(token);
const isRange = label === "range";
response[key] ??= isRange ? [] : value;
if (isRange) {
response[key] = response[key].concat(value);
Expand Down

0 comments on commit d0ee36f

Please sign in to comment.