Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add support for glossary and escaped variables to the variable parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Dom Harrington committed Jun 27, 2018
1 parent b99df5c commit 9d6b95e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,76 @@ it('should output a variable node', () => {

expect(remark().use(parser).data('settings', { position: false }).parse(markdown)).toEqual(ast);
});

it('should output a glossary node', () => {
const markdown = 'This is a test <<glossary:item>>.';
const ast = {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{ type: 'text', value: 'This is a test ' },
{
type: 'readme-glossary',
data: {
hName: 'readme-glossary',
hProperties: {
term: 'item',
},
},
},
{ type: 'text', value: '.' },
],
},
],
};

expect(remark().use(parser).data('settings', { position: false }).parse(markdown)).toEqual(ast);
});

it('should allow whitespace in glossary names', () => {
const markdown = 'This is a test <<glossary:item name>>.';
const ast = {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{ type: 'text', value: 'This is a test ' },
{
type: 'readme-glossary',
data: {
hName: 'readme-glossary',
hProperties: {
term: 'item name',
},
},
},
{ type: 'text', value: '.' },
],
},
],
};

expect(remark().use(parser).data('settings', { position: false }).parse(markdown)).toEqual(ast);
});

it('should allow escape variables to remain', () => {
const markdown = 'This is a test escaped key \\<<apiKey\\>>.';
const ast = {
type: 'root',
children: [
{
type: 'paragraph',
children: [
{ type: 'text', value: 'This is a test escaped key ' },
{ type: 'text', value: '<<apiKey>>' },
{ type: 'text', value: '.' },
],
},
],
};

expect(remark().use(parser).data('settings', { position: false }).parse(markdown)).toEqual(ast);
});
27 changes: 21 additions & 6 deletions packages/api-explorer/src/lib/markdown/variable-parser.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
function tokenizeVariable(eat, value, silent) {
const match = /^<<([-\w:]*?)>>/.exec(value);

if (!match) return false;
const match = /^<<([-\w:\s]+)>>/.exec(value);

if (!match) {
const escapedMatch = /^\\<<([-\w:]*?)\\>>/.exec(value);
if (escapedMatch) {
return eat(escapedMatch[0])({
type: 'text',
value: escapedMatch[0].replace(/\\/g, ''),
});
}
return false;
}

if (silent) return true;

if (match[1].startsWith('glossary:')) {
return eat(match[0])({
type: 'readme-glossary',
data: { hName: 'readme-glossary', hProperties: { term: match[1].replace('glossary:', '') } },
});
}

return eat(match[0])({
type: 'readme-variable',
data: { hName: 'readme-variable', hProperties: { variable: match[1] } },
});
}

// TODO i'm not sure what this does?
function locateMention(value, fromIndex) {
function locate(value, fromIndex) {
return value.indexOf('<<', fromIndex);
}

tokenizeVariable.locator = locateMention;
tokenizeVariable.locator = locate;

function parser() {
const Parser = this.Parser;
Expand Down

0 comments on commit 9d6b95e

Please sign in to comment.