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

fix: sort the children explicitly #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 22 additions & 8 deletions src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

const asciidoctor = require("asciidoctor.js")();

function sortChildrenByRange(children){
return children.slice().sort((childA, childB) => {
return childA.range[0] > childB.range[0] ? 1 : -1
});
}
class Converter {
convert(text) {
const doc = asciidoctor.load(text, { sourcemap: true });
Expand Down Expand Up @@ -239,15 +244,16 @@ class Converter {
if (children.length === 0) {
return [];
}
const sortedChildren = sortChildrenByRange(children);
const loc = {
start: children[0].loc.start,
end: children[children.length - 1].loc.end
start: sortedChildren[0].loc.start,
end: sortedChildren[sortedChildren.length - 1].loc.end
};
const range = this.locationToRange(loc);
return [
{
type: "TableRow",
children,
children:sortedChildren,
loc,
range,
raw: ""
Expand All @@ -266,15 +272,20 @@ class Converter {
if (children.length === 0) {
return [];
}
// In ListItem does not sort the loc in sometimes
// sort ascending order explicitly
const sortedChildren = children.slice().sort((childA, childB) => {
return childA.range[0] > childB.range[0] ? 1 : -1
});
const loc = {
start: children[0].loc.start,
end: children[children.length - 1].loc.end
start: sortedChildren[0].loc.start,
end: sortedChildren[sortedChildren.length - 1].loc.end
};
const range = this.locationToRange(loc);
return [
{
type: "Table",
children,
children: sortedChildren,
loc,
range,
raw: ""
Expand All @@ -300,9 +311,12 @@ class Converter {
}

locAndRangeFrom(children) {
// In ListItem does not sort the loc in sometimes
// sort ascending order explicitly
const sortedChildren = sortChildrenByRange(children);
const loc = {
start: children[0].loc.start,
end: children[children.length - 1].loc.end
start: sortedChildren[0].loc.start,
end: sortedChildren[sortedChildren.length - 1].loc.end
};
const range = this.locationToRange(loc);
return { loc, range };
Expand Down
Loading