Skip to content

Commit

Permalink
rewrite: fix rewriting list attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnthe committed Oct 30, 2018
1 parent 5f022e8 commit 28dd1f2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions __tests__/rewrite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ line cell
const input = `
[[my-list-id]]
.List Title
[role=my_role]
Operating Systems::
Linux:::
. Fedora
Expand Down
2 changes: 1 addition & 1 deletion extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function extractBlock(block: AbstractBlock, options: ExtractOptions= {}):
const attributeFilter = options.attributeFilter || defaultAttributeFilter;
const extractions = Object.keys(attributes).filter(attributeFilter).map(key => {
return {
text: attributes[key] as string,
text: attributes[key],
};
});
return extractions;
Expand Down
17 changes: 12 additions & 5 deletions rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function getAttributesString(block: AbstractBlock, transformer: RewriteTransform
continue;
}
const origValue = attributes[attributeKey];
if (attributeKey === 'attribute_entries' || typeof origValue !== 'string') {
if (attributeKey === 'attribute_entries') {
continue;
}
const isLocalizable = localizableKeys.includes(attributeKey);
Expand Down Expand Up @@ -114,7 +114,15 @@ function processCustomAttributes(block: AbstractBlock, transformer: RewriteTrans
function rewriteBlock(block: AbstractBlock, transformer: RewriteTransformer, write: Write, state: ReWriteState) {
const listRewrite = {
open: () => {
updateListStack('push', block, state, write);
const listBlock = block as Block;
updateListStack('push', listBlock, state, write);
const attributesString = getAttributesString(
listBlock, transformer, [], ['id', 'style']);
if (attributesString !== '') {
const style = listBlock.getStyle();
const styleStr = isNil(style) ? '' : `${style}, `;
write(`[${styleStr}${attributesString}]\n`);
}
},
close: () => {
updateListStack('pop', block, state, write);
Expand Down Expand Up @@ -145,7 +153,7 @@ function rewriteBlock(block: AbstractBlock, transformer: RewriteTransformer, wri
for (const key of document.attributes_modified.hash.$$keys) {
const isNonLocalizable = nonLocalizableBuiltinAttributeKeys.includes(key);
const originalValue = attributes[key];
if (originalValue === undefined) {
if (originalValue as any === undefined) {
continue;
}
const value = isNonLocalizable ? originalValue : transformer(originalValue);
Expand Down Expand Up @@ -256,7 +264,7 @@ function rewriteBlock(block: AbstractBlock, transformer: RewriteTransformer, wri
extraAttributes.caption = table.caption;
}
const attrs = table.getAttributes();
if (attrs.cols === undefined) {
if (attrs.cols as any === undefined) {
// Synthesize 'cols' attribute if it's missing. For some reason, if the table starts with
// colspans, the colcount is sometimes otherwise incorrectly calculated. Not sure why. Asciidoctor bug?
extraAttributes.cols = `${attrs.colcount}*`;
Expand All @@ -276,7 +284,6 @@ function rewriteBlock(block: AbstractBlock, transformer: RewriteTransformer, wri
const text = escapePipes(transformer(cell.text));
write(`${colspan}.${rowspan}+|${text}\n`);
}
write('\n');
}
}
write(`|===\n`);
Expand Down
2 changes: 1 addition & 1 deletion types/asciidoctor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ declare namespace AsciiDoctorJs {
}

export type Attributes = {
[key: string]: string | undefined;
[key: string]: string;
}

export interface OpalHash {
Expand Down

0 comments on commit 28dd1f2

Please sign in to comment.