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(core/inlines): add new syntax for inline <var> #2175

Merged
merged 8 commits into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/core/inlines.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ function inlineAbbrMatches(matched, txt, abbrMap) {
: hyperHTML`<abbr title="${abbrMap.get(matched)}">${matched}</abbr>`;
}

/**
* @example |varName: type| => <var data-type="type">varName</var>
* @example |varName| => <var>varName</var>
* @param {string} matched
*/
function inlineVariableMatches(matched) {
// remove "|" at the beginning and at the end, then split at an optional `:`
const matches = matched.slice(1, -1).split(":", 2);
const [varName, type] = matches.map(s => s.trim());
return hyperHTML`<var data-type="${type}">${varName}</var>`;
}

export function run(conf) {
document.normalize();
if (!document.querySelector("section#conformance")) {
Expand Down Expand Up @@ -118,6 +130,7 @@ export function run(conf) {
"\\b(?:NOT\\s+)?RECOMMENDED\\b",
"\\bOPTIONAL\\b",
"(?:{{3}\\s*.*\\s*}{3})", // inline IDL references,
"\\B\\|\\w+(?:\\s*\\w+)*(?:\\:\\s*\\w+)?\\|\\B", // inline variable regex
marcoscaceres marked this conversation as resolved.
Show resolved Hide resolved
sidvishnoi marked this conversation as resolved.
Show resolved Hide resolved
"(?:\\[\\[(?:!|\\\\|\\?)?[A-Za-z0-9\\.-]+\\]\\])",
...(abbrRx ? [abbrRx] : []),
].join("|")})`
Expand All @@ -138,6 +151,9 @@ export function run(conf) {
} else if (t.startsWith("[[")) {
const nodes = inlineBibrefMatches(t, txt, conf);
df.append(...nodes);
} else if (t.startsWith("|") && t.endsWith("|")) {
marcoscaceres marked this conversation as resolved.
Show resolved Hide resolved
const node = inlineVariableMatches(t);
df.appendChild(node);
} else if (abbrMap.has(t)) {
const node = inlineAbbrMatches(t, txt, abbrMap);
df.appendChild(node);
Expand Down
28 changes: 28 additions & 0 deletions tests/spec/core/inlines-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,32 @@ describe("Core - Inlines", () => {
expect(rfc2119[0].textContent).toBe("MUST");
expect(rfc2119[1].textContent).toBe("NOT RECOMMENDED");
});

it("processes inline variable syntax", async () => {
const body = `
<section>
<p id="a1">TEXT |variable: Type| TEXT</p>
<p id="a2">TEXT |variable with spaces:Type| TEXT</p>
<p id="b">TEXT |variable| TEXT</p>
<p id="c">TEXT | ignored | TEXT</p>
marcoscaceres marked this conversation as resolved.
Show resolved Hide resolved
<p id="d">TEXT|ignore: Ignore|TEXT</p>
</section>
`;
const doc = await makeRSDoc(makeStandardOps(null, body));

const a1 = doc.querySelector("#a1 var");
expect(a1.textContent).toEqual("variable");
expect(a1.dataset.type).toEqual("Type");

const a2 = doc.querySelector("#a2 var");
expect(a2.textContent).toEqual("variable with spaces");
expect(a2.dataset.type).toEqual("Type");

const b = doc.querySelector("#b var");
expect(b.textContent).toEqual("variable");
expect(b.dataset.type).toBeUndefined();

expect(doc.querySelector("#c var")).toBeFalsy();
expect(doc.querySelector("#d var")).toBeFalsy();
});
});