refactor: remove unnecessary complexity

pull/5679/head
Geoff Rich 5 years ago
parent 71bec3a51d
commit a9489e1f79

@ -112,7 +112,7 @@ export default function() {
} }
return ` return `
<h${level} ${extracted.attrstring}> <h${level}>
<span id="${slug}" class="offset-anchor" ${level > 4 || tocIgnore ? 'data-scrollignore' : ''}></span> <span id="${slug}" class="offset-anchor" ${level > 4 || tocIgnore ? 'data-scrollignore' : ''}></span>
<a href="docs#${slug}" class="anchor" aria-hidden="true"></a> <a href="docs#${slug}" class="anchor" aria-hidden="true"></a>
${text} ${text}

@ -1,16 +1,15 @@
const ATTRS_REGEX = /^(.+)\s+\{(.+)\}$/; const ATTRS_REGEX = /^(.+)\s+\{(.+)\}$/;
/** /**
* Extracts attributes from markdown text to be applied to the resulting HTML. * Extracts attributes from Markdown text.
* @example * @example
* // returns { * // returns {
* // text: 'Heading', * // text: 'Heading',
* // raw: '<code>Heading</code>', * // raw: '<code>Heading</code>',
* // attrs: { id: 'important', class: 'red', test: 'true' }, * // attrs: { test: 'true' }
* // attrstring: 'id="important" class="red" test="true"'
* // } * // }
* extract_attributes( * extract_attributes(
* 'Heading { #important .red test=true }', * 'Heading { test=true }',
* '<code>Heading</code> { #important .red test=true }' * '<code>Heading</code> { test=true }'
* ); * );
* ``` * ```
* @param {string} text * @param {string} text
@ -25,16 +24,14 @@ export function extract_attributes(text, raw) {
return { return {
text: textMatch ? textMatch[1] : text, text: textMatch ? textMatch[1] : text,
raw: rawMatch ? rawMatch[1] : raw, raw: rawMatch ? rawMatch[1] : raw,
attrs, attrs
attrstring: Object.keys(attrs).map(key => `${key}="${attrs[key].trim()}"`).join(' ')
} }
} catch (err) { } catch (err) {
console.log(err); console.log(err);
return { return {
text, text,
raw, raw,
attrs: {}, attrs: {}
attrstring: ''
}; };
} }
} }
@ -44,18 +41,9 @@ function parse_attributes(raw_attributes) {
const result = { }; const result = { };
attributes.forEach(attr => { attributes.forEach(attr => {
if (!attr) return; if (!attr) return;
if (attr.startsWith('#')) {
result.id = attr.substring(1);
} else if (attr.startsWith('.')) {
if (!result.class) {
result.class = '';
}
result.class += attr.substring(1) + ' ';
} else {
let [key, value = ''] = attr.split('='); let [key, value = ''] = attr.split('=');
result[key] = value; result[key] = value;
} });
})
return result; return result;
} }

Loading…
Cancel
Save