Skip to content

Commit

Permalink
Reuse!
Browse files Browse the repository at this point in the history
  • Loading branch information
HeikoTheissen committed Oct 10, 2024
1 parent 3bf41f1 commit 7ca683f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
56 changes: 29 additions & 27 deletions lib/csdl2markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,14 +612,9 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
* @return {string} Text
*/
function applicableTermsList(applicableTerms) {
const text = [];
if (applicableTerms.length > 0)
text.push("<br>Applicable Annotation Terms:<ul>");
applicableTerms.forEach((term) => {
text.push(`<li>${typeLink({ $Type: term })}</li>`);
});
if (applicableTerms.length > 0) text.push("</ul>");
return text.join("");
return applicableTerms.length > 0
? "<br>Applicable Annotation Terms:" + linkList(applicableTerms)
: "";
}

/**
Expand All @@ -628,13 +623,9 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
* @return {string} Text
*/
function allowedTermsList(allowedTerms) {
const text = [];
if (allowedTerms.length > 0) text.push("<br>Allowed terms:<ul>");
allowedTerms.forEach((term) => {
text.push(`<li>${typeLink({ $Type: term })}</li>`);
});
if (allowedTerms.length > 0) text.push("</ul>");
return text.join("");
return allowedTerms.length > 0
? "<br>Allowed Terms:" + linkList(allowedTerms)
: "";
}

/**
Expand Down Expand Up @@ -745,21 +736,32 @@ module.exports.csdl2markdown = function (filename, csdl, referenced = {}) {
(modelElement.$Nullable ? "?" : "") +
(customType ? "](" + customFile + "#" + np.name + ")" : "") +
(modelElement.$Collection ? "\\]" : "");
if (modelElement[voc.Validation.DerivedTypeConstraint]) {
type += "<br>Allowed derived types:<ul>";
for (let dt of modelElement[voc.Validation.DerivedTypeConstraint]) {
let link;
if (dt.startsWith("Collection(")) {
dt = dt.substring(11, dt.length - 1);
link = "[.]";
} else link = ".";
type += `<li>${link.replace(".", typeLink({ $Type: dt }))}</li>`;
}
type += "</ul>";
}
if (modelElement[voc.Validation.DerivedTypeConstraint])
type +=
"<br>Allowed Derived Types:" +
linkList(modelElement[voc.Validation.DerivedTypeConstraint]);
return type;
}

/**
* a qualified name consists of a namespace or alias, a dot, and a simple name
* @param {object} list of model element names
* @return {string} list of links to these model elements
*/
function linkList(list) {
let text = "<ul>";
for (let t of list) {
let link;
if (t.startsWith("Collection(")) {
t = t.substring(11, t.length - 1);
link = "[.]";
} else link = ".";
text += `<li>${link.replace(".", typeLink({ $Type: t }))}</li>`;
}
text += "</ul>";
return text;
}

/**
* a qualified name consists of a namespace or alias, a dot, and a simple name
* @param {string} qualifiedName
Expand Down
4 changes: 2 additions & 2 deletions test/csdl2markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ describe("Non-OASIS Vocabularies", function () {
"",
"Term|Type|Description",
":---|:---|:----------",
'Reference|AnnotationPath|<a name="Reference"></a>Reference to a description<br>Allowed terms:<ul><li>[Description](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Description)</li><li>[WithoutReference](#WithoutReference)</li></ul>',
'Reference|AnnotationPath|<a name="Reference"></a>Reference to a description<br>Allowed Terms:<ul><li>[Description](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Description)</li><li>[WithoutReference](#WithoutReference)</li></ul>',
"",
];
const markdown = lib.csdl2markdown(filename, vocabulary);
Expand Down Expand Up @@ -568,7 +568,7 @@ describe("Edge cases", function () {
],
};
const mdtype =
"PrimitiveType<br>Allowed derived types:<ul><li>[Tag](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Tag)</li><li>Duration</li></ul>";
"PrimitiveType<br>Allowed Derived Types:<ul><li>[Tag](https://github.com/oasis-tcs/odata-vocabularies/blob/main/vocabularies/Org.OData.Core.V1.md#Tag)</li><li>Duration</li></ul>";
const vocabulary = {
$Version: "4.01",
$Reference: {
Expand Down

0 comments on commit 7ca683f

Please sign in to comment.