@markdoc/markdoc 0.1.1 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -4
- package/dist/index.d.ts +38 -21
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +108 -36
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +108 -36
- package/dist/index.mjs.map +2 -2
- package/dist/src/ast/node.d.ts +2 -1
- package/dist/src/ast/node.d.ts.map +1 -1
- package/dist/src/parser.d.ts.map +1 -1
- package/dist/src/schema.d.ts.map +1 -1
- package/dist/src/tags/partial.d.ts.map +1 -1
- package/dist/src/tokenizer/index.d.ts.map +1 -1
- package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
- package/dist/src/transformer.d.ts +1 -7
- package/dist/src/transformer.d.ts.map +1 -1
- package/dist/src/types.d.ts +10 -5
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/utils.d.ts +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/global.d.ts +7 -0
- package/index.ts +37 -19
- package/package.json +6 -4
- package/src/ast/node.test.ts +52 -0
- package/src/ast/node.ts +3 -0
- package/src/parser.test.ts +42 -0
- package/src/parser.ts +33 -3
- package/src/schema.ts +1 -0
- package/src/tags/partial.ts +20 -3
- package/src/tokenizer/index.ts +5 -1
- package/src/tokenizer/plugins/annotations.test.ts +60 -0
- package/src/tokenizer/plugins/annotations.ts +4 -0
- package/src/transformer.ts +17 -16
- package/src/transforms/table.ts +1 -1
- package/src/types.ts +11 -5
- package/src/utils.test.ts +15 -2
- package/src/utils.ts +10 -1
- package/src/validator.test.ts +36 -10
- package/src/validator.ts +11 -11
package/dist/index.mjs
CHANGED
|
@@ -6707,6 +6707,7 @@ function findTagEnd(content, start = 0) {
|
|
|
6707
6707
|
return pos;
|
|
6708
6708
|
}
|
|
6709
6709
|
}
|
|
6710
|
+
return null;
|
|
6710
6711
|
}
|
|
6711
6712
|
function parseTag(content, line, contentStart) {
|
|
6712
6713
|
try {
|
|
@@ -6736,7 +6737,11 @@ function parseTags(content, firstLine = 0) {
|
|
|
6736
6737
|
}
|
|
6737
6738
|
if (!content.startsWith(OPEN, pos))
|
|
6738
6739
|
continue;
|
|
6739
|
-
const end = findTagEnd(content, pos)
|
|
6740
|
+
const end = findTagEnd(content, pos);
|
|
6741
|
+
if (end == null) {
|
|
6742
|
+
pos = pos + OPEN.length;
|
|
6743
|
+
continue;
|
|
6744
|
+
}
|
|
6740
6745
|
const text2 = content.slice(pos, end + CLOSE.length);
|
|
6741
6746
|
const inner = content.slice(pos + OPEN.length, end);
|
|
6742
6747
|
const lineStart = content.lastIndexOf("\n", pos);
|
|
@@ -6782,6 +6787,9 @@ function createToken(state, content, contentStart) {
|
|
|
6782
6787
|
const token = state.push(type, "", nesting);
|
|
6783
6788
|
token.info = content;
|
|
6784
6789
|
token.meta = meta;
|
|
6790
|
+
if (!state.delimiters) {
|
|
6791
|
+
state.delimiters = [];
|
|
6792
|
+
}
|
|
6785
6793
|
return token;
|
|
6786
6794
|
} catch (error2) {
|
|
6787
6795
|
if (!(error2 instanceof import_tag2.SyntaxError))
|
|
@@ -6904,7 +6912,10 @@ var Tokenizer = class {
|
|
|
6904
6912
|
this.parser = new import_lib.default(config);
|
|
6905
6913
|
this.parser.use(plugin, "annotations", {});
|
|
6906
6914
|
this.parser.use(plugin2, "frontmatter", {});
|
|
6907
|
-
this.parser.disable(
|
|
6915
|
+
this.parser.disable([
|
|
6916
|
+
"lheading",
|
|
6917
|
+
"code"
|
|
6918
|
+
]);
|
|
6908
6919
|
}
|
|
6909
6920
|
tokenize(content) {
|
|
6910
6921
|
return this.parser.parse(content.toString(), {});
|
|
@@ -6946,6 +6957,9 @@ var Class = class {
|
|
|
6946
6957
|
};
|
|
6947
6958
|
|
|
6948
6959
|
// src/transformer.ts
|
|
6960
|
+
function isPromise(a) {
|
|
6961
|
+
return a && typeof a === "object" && typeof a.then === "function";
|
|
6962
|
+
}
|
|
6949
6963
|
var globalAttributes = {
|
|
6950
6964
|
class: { type: Class, render: true },
|
|
6951
6965
|
id: { type: String, render: true }
|
|
@@ -6977,7 +6991,11 @@ var transformer_default = {
|
|
|
6977
6991
|
return output;
|
|
6978
6992
|
},
|
|
6979
6993
|
children(node, config = {}) {
|
|
6980
|
-
|
|
6994
|
+
const children = node.children.flatMap((child) => this.node(child, config));
|
|
6995
|
+
if (children.some(isPromise)) {
|
|
6996
|
+
return Promise.all(children);
|
|
6997
|
+
}
|
|
6998
|
+
return children;
|
|
6981
6999
|
},
|
|
6982
7000
|
node(node, config = {}) {
|
|
6983
7001
|
const schema = this.findSchema(node, config) ?? {};
|
|
@@ -6987,6 +7005,9 @@ var transformer_default = {
|
|
|
6987
7005
|
if (!schema || !schema.render)
|
|
6988
7006
|
return children;
|
|
6989
7007
|
const attributes = this.attributes(node, config);
|
|
7008
|
+
if (isPromise(attributes) || isPromise(children)) {
|
|
7009
|
+
return Promise.all([attributes, children]).then((values) => new Tag(schema.render, ...values));
|
|
7010
|
+
}
|
|
6990
7011
|
return new Tag(schema.render, attributes, children);
|
|
6991
7012
|
}
|
|
6992
7013
|
};
|
|
@@ -7002,6 +7023,7 @@ var Node = class {
|
|
|
7002
7023
|
this.children = children;
|
|
7003
7024
|
this.type = type;
|
|
7004
7025
|
this.tag = tag;
|
|
7026
|
+
this.annotations = [];
|
|
7005
7027
|
}
|
|
7006
7028
|
*walk() {
|
|
7007
7029
|
for (const child of this.children) {
|
|
@@ -7078,7 +7100,7 @@ function transform(document2) {
|
|
|
7078
7100
|
for (const row of rest) {
|
|
7079
7101
|
if (row.type === "list")
|
|
7080
7102
|
convertToRow(row);
|
|
7081
|
-
else if (row.type === "tag" && row.tag
|
|
7103
|
+
else if (row.type === "tag" && row.tag === "if") {
|
|
7082
7104
|
const children = [];
|
|
7083
7105
|
for (const child of row.children) {
|
|
7084
7106
|
if (child.type === "hr")
|
|
@@ -7108,7 +7130,9 @@ var mappings = {
|
|
|
7108
7130
|
variable: "text"
|
|
7109
7131
|
};
|
|
7110
7132
|
function annotate(node, attributes) {
|
|
7111
|
-
for (const
|
|
7133
|
+
for (const attribute of attributes) {
|
|
7134
|
+
node.annotations.push(attribute);
|
|
7135
|
+
const { name, value, type } = attribute;
|
|
7112
7136
|
if (type === "attribute")
|
|
7113
7137
|
node.attributes[name] = value;
|
|
7114
7138
|
else if (type === "class")
|
|
@@ -7116,6 +7140,7 @@ function annotate(node, attributes) {
|
|
|
7116
7140
|
node.attributes.class[name] = value;
|
|
7117
7141
|
else
|
|
7118
7142
|
node.attributes.class = { [name]: value };
|
|
7143
|
+
}
|
|
7119
7144
|
}
|
|
7120
7145
|
function handleAttrs(token, type) {
|
|
7121
7146
|
switch (type) {
|
|
@@ -7125,11 +7150,11 @@ function handleAttrs(token, type) {
|
|
|
7125
7150
|
return { ordered: token.type.startsWith("ordered") };
|
|
7126
7151
|
case "link": {
|
|
7127
7152
|
const attrs = Object.fromEntries(token.attrs);
|
|
7128
|
-
return { href: attrs.href };
|
|
7153
|
+
return attrs.title ? { href: attrs.href, title: attrs.title } : { href: attrs.href };
|
|
7129
7154
|
}
|
|
7130
7155
|
case "image": {
|
|
7131
7156
|
const attrs = Object.fromEntries(token.attrs);
|
|
7132
|
-
return { alt: token.content, src: attrs.src };
|
|
7157
|
+
return attrs.title ? { alt: token.content, src: attrs.src, title: attrs.title } : { alt: token.content, src: attrs.src };
|
|
7133
7158
|
}
|
|
7134
7159
|
case "text":
|
|
7135
7160
|
case "code":
|
|
@@ -7138,6 +7163,26 @@ function handleAttrs(token, type) {
|
|
|
7138
7163
|
const [language] = token.info.split(" ", 1);
|
|
7139
7164
|
return language === "" ? { content: token.content } : { content: token.content, language };
|
|
7140
7165
|
}
|
|
7166
|
+
case "td":
|
|
7167
|
+
case "th": {
|
|
7168
|
+
if (token.attrs) {
|
|
7169
|
+
const attrs = Object.fromEntries(token.attrs);
|
|
7170
|
+
let align;
|
|
7171
|
+
if (attrs.style) {
|
|
7172
|
+
if (attrs.style.includes("left")) {
|
|
7173
|
+
align = "left";
|
|
7174
|
+
} else if (attrs.style.includes("center")) {
|
|
7175
|
+
align = "center";
|
|
7176
|
+
} else if (attrs.style.includes("right")) {
|
|
7177
|
+
align = "right";
|
|
7178
|
+
}
|
|
7179
|
+
}
|
|
7180
|
+
if (align) {
|
|
7181
|
+
return { align };
|
|
7182
|
+
}
|
|
7183
|
+
}
|
|
7184
|
+
return {};
|
|
7185
|
+
}
|
|
7141
7186
|
default:
|
|
7142
7187
|
return {};
|
|
7143
7188
|
}
|
|
@@ -7294,7 +7339,8 @@ var image = {
|
|
|
7294
7339
|
render: "img",
|
|
7295
7340
|
attributes: {
|
|
7296
7341
|
src: { type: String, required: true },
|
|
7297
|
-
alt: { type: String }
|
|
7342
|
+
alt: { type: String },
|
|
7343
|
+
title: { type: String }
|
|
7298
7344
|
}
|
|
7299
7345
|
};
|
|
7300
7346
|
var fence2 = {
|
|
@@ -7494,10 +7540,25 @@ var tagElse = {
|
|
|
7494
7540
|
};
|
|
7495
7541
|
|
|
7496
7542
|
// src/tags/partial.ts
|
|
7543
|
+
var PartialFile = class {
|
|
7544
|
+
validate(file, config) {
|
|
7545
|
+
const { partials = {} } = config;
|
|
7546
|
+
const partial2 = partials[file];
|
|
7547
|
+
if (!partial2)
|
|
7548
|
+
return [
|
|
7549
|
+
{
|
|
7550
|
+
id: "attribute-value-invalid",
|
|
7551
|
+
level: "error",
|
|
7552
|
+
message: `Partial \`${file}\` not found. The 'file' attribute must be set in \`config.partials\``
|
|
7553
|
+
}
|
|
7554
|
+
];
|
|
7555
|
+
return [];
|
|
7556
|
+
}
|
|
7557
|
+
};
|
|
7497
7558
|
var partial = {
|
|
7498
7559
|
selfClosing: true,
|
|
7499
7560
|
attributes: {
|
|
7500
|
-
file: { type:
|
|
7561
|
+
file: { type: PartialFile, render: false, required: true },
|
|
7501
7562
|
variables: { type: Object, render: false }
|
|
7502
7563
|
},
|
|
7503
7564
|
transform(node, config) {
|
|
@@ -7737,7 +7798,7 @@ function validateFunction(fn, config) {
|
|
|
7737
7798
|
{
|
|
7738
7799
|
id: "function-undefined",
|
|
7739
7800
|
level: "critical",
|
|
7740
|
-
message: `Undefined function '${fn.name}'`
|
|
7801
|
+
message: `Undefined function: '${fn.name}'`
|
|
7741
7802
|
}
|
|
7742
7803
|
];
|
|
7743
7804
|
if (schema.validate)
|
|
@@ -7749,7 +7810,7 @@ function validateFunction(fn, config) {
|
|
|
7749
7810
|
errors.push({
|
|
7750
7811
|
id: "parameter-undefined",
|
|
7751
7812
|
level: "error",
|
|
7752
|
-
message: `Invalid parameter '${key}'`
|
|
7813
|
+
message: `Invalid parameter: '${key}'`
|
|
7753
7814
|
});
|
|
7754
7815
|
continue;
|
|
7755
7816
|
}
|
|
@@ -7761,7 +7822,7 @@ function validateFunction(fn, config) {
|
|
|
7761
7822
|
errors.push({
|
|
7762
7823
|
id: "parameter-type-invalid",
|
|
7763
7824
|
level: "error",
|
|
7764
|
-
message: `Parameter '${key}' of '${fn.name}' must be type '${typeToString(param.type)}'`
|
|
7825
|
+
message: `Parameter '${key}' of '${fn.name}' must be type of '${typeToString(param.type)}'`
|
|
7765
7826
|
});
|
|
7766
7827
|
} else if (Array.isArray(valid)) {
|
|
7767
7828
|
errors.push(...valid);
|
|
@@ -7774,7 +7835,7 @@ function validateFunction(fn, config) {
|
|
|
7774
7835
|
errors.push({
|
|
7775
7836
|
id: "parameter-missing-required",
|
|
7776
7837
|
level: "error",
|
|
7777
|
-
message: `Missing required parameter '${key}'`
|
|
7838
|
+
message: `Missing required parameter: '${key}'`
|
|
7778
7839
|
});
|
|
7779
7840
|
return errors;
|
|
7780
7841
|
}
|
|
@@ -7785,7 +7846,7 @@ function validate(node, config) {
|
|
|
7785
7846
|
errors.push({
|
|
7786
7847
|
id: node.tag ? "tag-undefined" : "node-undefined",
|
|
7787
7848
|
level: "critical",
|
|
7788
|
-
message: node.tag ? `Undefined tag '${node.tag}'` : `Undefined node '${node.type}'`
|
|
7849
|
+
message: node.tag ? `Undefined tag: '${node.tag}'` : `Undefined node: '${node.type}'`
|
|
7789
7850
|
});
|
|
7790
7851
|
return errors;
|
|
7791
7852
|
}
|
|
@@ -7793,7 +7854,7 @@ function validate(node, config) {
|
|
|
7793
7854
|
errors.push({
|
|
7794
7855
|
id: "tag-selfclosing-has-children",
|
|
7795
7856
|
level: "critical",
|
|
7796
|
-
message: `
|
|
7857
|
+
message: `'${node.tag}' tag should be self-closing`
|
|
7797
7858
|
});
|
|
7798
7859
|
const attributes = {
|
|
7799
7860
|
...globalAttributes,
|
|
@@ -7807,7 +7868,7 @@ function validate(node, config) {
|
|
|
7807
7868
|
errors.push({
|
|
7808
7869
|
id: "attribute-undefined",
|
|
7809
7870
|
level: "error",
|
|
7810
|
-
message: `Invalid attribute '${key}'`
|
|
7871
|
+
message: `Invalid attribute: '${key}'`
|
|
7811
7872
|
});
|
|
7812
7873
|
continue;
|
|
7813
7874
|
}
|
|
@@ -7823,7 +7884,7 @@ function validate(node, config) {
|
|
|
7823
7884
|
errors.push({
|
|
7824
7885
|
id: "attribute-value-invalid",
|
|
7825
7886
|
level: "error",
|
|
7826
|
-
message: "The
|
|
7887
|
+
message: "The id attribute must not start with a number"
|
|
7827
7888
|
});
|
|
7828
7889
|
if (type) {
|
|
7829
7890
|
const valid = validateType(type, value, config);
|
|
@@ -7831,7 +7892,7 @@ function validate(node, config) {
|
|
|
7831
7892
|
errors.push({
|
|
7832
7893
|
id: "attribute-type-invalid",
|
|
7833
7894
|
level: errorLevel || "error",
|
|
7834
|
-
message: `Attribute '${key}' must be type '${typeToString(type)}'`
|
|
7895
|
+
message: `Attribute '${key}' must be type of '${typeToString(type)}'`
|
|
7835
7896
|
});
|
|
7836
7897
|
}
|
|
7837
7898
|
if (Array.isArray(valid)) {
|
|
@@ -7858,7 +7919,7 @@ function validate(node, config) {
|
|
|
7858
7919
|
errors.push({
|
|
7859
7920
|
id: "attribute-missing-required",
|
|
7860
7921
|
level: "error",
|
|
7861
|
-
message: `Missing required attribute '${key}'`
|
|
7922
|
+
message: `Missing required attribute: '${key}'`
|
|
7862
7923
|
});
|
|
7863
7924
|
for (const { type } of node.children) {
|
|
7864
7925
|
if (schema.children && type !== "error" && !schema.children.includes(type))
|
|
@@ -7921,30 +7982,40 @@ function validate2(content, options) {
|
|
|
7921
7982
|
function createElement(name, attributes = {}, ...children) {
|
|
7922
7983
|
return { name, attributes, children };
|
|
7923
7984
|
}
|
|
7924
|
-
var
|
|
7925
|
-
|
|
7926
|
-
|
|
7927
|
-
|
|
7928
|
-
|
|
7929
|
-
|
|
7930
|
-
|
|
7931
|
-
|
|
7932
|
-
Tokenizer,
|
|
7933
|
-
parseTags,
|
|
7934
|
-
transformer: transformer_default,
|
|
7935
|
-
validator: validate,
|
|
7936
|
-
parse: parse3,
|
|
7937
|
-
transform: transform2,
|
|
7938
|
-
validate: validate2,
|
|
7939
|
-
createElement
|
|
7985
|
+
var Markdoc = class {
|
|
7986
|
+
constructor(config) {
|
|
7987
|
+
this.parse = parse3;
|
|
7988
|
+
this.resolve = (content) => resolve2(content, this.config);
|
|
7989
|
+
this.transform = (content) => transform2(content, this.config);
|
|
7990
|
+
this.validate = (content) => validate2(content, this.config);
|
|
7991
|
+
this.config = config;
|
|
7992
|
+
}
|
|
7940
7993
|
};
|
|
7994
|
+
Markdoc.nodes = schema_exports;
|
|
7995
|
+
Markdoc.tags = tags_default;
|
|
7996
|
+
Markdoc.functions = functions_default;
|
|
7997
|
+
Markdoc.globalAttributes = globalAttributes;
|
|
7998
|
+
Markdoc.renderers = renderers_default;
|
|
7999
|
+
Markdoc.transforms = transforms_default;
|
|
8000
|
+
Markdoc.Ast = ast_default;
|
|
8001
|
+
Markdoc.Tag = Tag;
|
|
8002
|
+
Markdoc.Tokenizer = Tokenizer;
|
|
8003
|
+
Markdoc.parseTags = parseTags;
|
|
8004
|
+
Markdoc.transformer = transformer_default;
|
|
8005
|
+
Markdoc.validator = validate;
|
|
8006
|
+
Markdoc.parse = parse3;
|
|
8007
|
+
Markdoc.transform = transform2;
|
|
8008
|
+
Markdoc.validate = validate2;
|
|
8009
|
+
Markdoc.createElement = createElement;
|
|
8010
|
+
Markdoc.truthy = truthy;
|
|
7941
8011
|
export {
|
|
7942
8012
|
ast_default as Ast,
|
|
7943
8013
|
Tag,
|
|
7944
8014
|
Tokenizer,
|
|
7945
8015
|
createElement,
|
|
7946
|
-
|
|
8016
|
+
Markdoc as default,
|
|
7947
8017
|
functions_default as functions,
|
|
8018
|
+
globalAttributes,
|
|
7948
8019
|
schema_exports as nodes,
|
|
7949
8020
|
parse3 as parse,
|
|
7950
8021
|
parseTags,
|
|
@@ -7954,6 +8025,7 @@ export {
|
|
|
7954
8025
|
transform2 as transform,
|
|
7955
8026
|
transformer_default as transformer,
|
|
7956
8027
|
transforms_default as transforms,
|
|
8028
|
+
truthy,
|
|
7957
8029
|
validate2 as validate,
|
|
7958
8030
|
validate as validator
|
|
7959
8031
|
};
|