
This processor will eventually replace the `{@example}` inline tags because it provides a cleaner approach that also supports tabbed examples straight out of the box. The idea is that authors will simply add a `path` and (optionally) a `region` attribute to `<code-example>` or `<code-pane>` elements in their docs. This indicates to dgeni that the relevant example needs to be injected into the content of this element. For example, assume that there is an example file `toh-pt1/index.hml` with a region called `title`, which looks like: ``` <h1>Tour of Heroes</h1> ``` Then the document author could get this to appear in the docs as a standalone example: ``` <code-example path="toh-pt1" region="title"></code-example> ``` Or as part of a tabbed group: ``` <code-tabs> <code-pane path="toh-pt1" region="title"></code-pane> </code-tabs> ``` If no `path` attribute is provided then the element is ignored, which enables authors to provide inline code instead: ``` <code-example> Some <html> escaped code </code-example> ``` Also all attributes other than `path` and `region` are ignored and passed through to the final rendered output allowing the author to provide styling hints: ``` <code-example path="toh-pt1" region="title" linenums"15" class="important"> </code-example> ```
76 lines
2.5 KiB
JavaScript
76 lines
2.5 KiB
JavaScript
const { mapObject, parseAttributes } = require('./utils');
|
|
|
|
fdescribe('utils', () => {
|
|
describe('mapObject', () => {
|
|
it('creates a new object', () => {
|
|
const testObj = { a: 1 };
|
|
const mappedObj = mapObject(testObj, (key, value) => value);
|
|
expect(mappedObj).toEqual(testObj);
|
|
expect(mappedObj).not.toBe(testObj);
|
|
});
|
|
|
|
it('maps the values via the mapper function', () => {
|
|
const testObj = { a: 1, b: 2 };
|
|
const mappedObj = mapObject(testObj, (key, value) => value * 2);
|
|
expect(mappedObj).toEqual({ a: 2, b: 4 });
|
|
});
|
|
});
|
|
|
|
describe('parseAttributes', () => {
|
|
it('should parse empty string', () => {
|
|
const attrs = parseAttributes('');
|
|
expect(attrs).toEqual({ });
|
|
});
|
|
|
|
it('should parse blank string', () => {
|
|
const attrs = parseAttributes(' ');
|
|
expect(attrs).toEqual({ });
|
|
});
|
|
|
|
it('should parse double quoted attributes', () => {
|
|
const attrs = parseAttributes('a="one" b="two"');
|
|
expect(attrs).toEqual({ a: 'one', b: 'two' });
|
|
});
|
|
|
|
it('should parse empty quoted attributes', () => {
|
|
const attrs = parseAttributes('a="" b="two"');
|
|
expect(attrs).toEqual({ a: '', b: 'two' });
|
|
});
|
|
|
|
it('should parse single quoted attributes', () => {
|
|
const attrs = parseAttributes('a=\'one\' b=\'two\'');
|
|
expect(attrs).toEqual({ a: 'one', b: 'two' });
|
|
});
|
|
|
|
it('should ignore whitespace', () => {
|
|
const attrs = parseAttributes(' a = "one" b = "two" ');
|
|
expect(attrs).toEqual({ a: 'one', b: 'two' });
|
|
});
|
|
|
|
it('should parse attributes with quotes within quotes', () => {
|
|
const attrs = parseAttributes('a=\'o"n"e\' b="t\'w\'o"');
|
|
expect(attrs).toEqual({ a: 'o"n"e', b: 't\'w\'o' });
|
|
});
|
|
|
|
it('should parse attributes with spaces in their values', () => {
|
|
const attrs = parseAttributes('a="one and two" b="three and four"');
|
|
expect(attrs).toEqual({ a: 'one and two', b: 'three and four' });
|
|
});
|
|
|
|
it('should parse empty attributes', () => {
|
|
const attrs = parseAttributes('a b="two"');
|
|
expect(attrs).toEqual({ a: true, b: 'two' });
|
|
});
|
|
|
|
it('should parse unquoted attributes', () => {
|
|
const attrs = parseAttributes('a=one b=two');
|
|
expect(attrs).toEqual({ a: 'one', b: 'two' });
|
|
});
|
|
|
|
it('should complain if a quoted attribute is not closed', () => {
|
|
expect(() => parseAttributes('a="" b="two')).toThrowError(
|
|
'Unterminated quoted attribute value in `a="" b="two`. Starting at 8. Expected a " but got "end of string".'
|
|
);
|
|
})
|
|
});
|
|
}); |