Skip to content
Open
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
46 changes: 38 additions & 8 deletions dev/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ function compiler(options) {
let firstBlankLineIndex
/** @type {boolean | undefined} */
let atMarker
/** @type {Array<number>} */
const insertPositions = []
/** @type {Array<Event>} */
const insertEvents = []

while (++index <= length) {
const event = events[index]
Expand Down Expand Up @@ -371,7 +375,7 @@ function compiler(options) {
let tailIndex = index
lineIndex = undefined

while (tailIndex--) {
while (tailIndex-- > start) {
const tailEvent = events[tailIndex]

if (
Expand Down Expand Up @@ -413,9 +417,8 @@ function compiler(options) {
lineIndex ? events[lineIndex][1].start : event[1].end
)

events.splice(lineIndex || index, 0, ['exit', listItem, event[2]])
index++
length++
insertPositions.push(lineIndex || index)
insertEvents.push(['exit', listItem, event[2]])
}

// Create a new list item.
Expand All @@ -429,17 +432,44 @@ function compiler(options) {
end: undefined
}
listItem = item
events.splice(index, 0, ['enter', item, event[2]])
index++
length++
insertPositions.push(index)
insertEvents.push(['enter', item, event[2]])
firstBlankLineIndex = undefined
atMarker = true
}
}
}

// Apply deferred insertions in a single backward merge pass.
const insertCount = insertPositions.length

if (insertCount > 0) {
const previousLength = events.length
const rangeEnd = length
events.length = previousLength + insertCount

// Shift the tail (events after the list range) to make room.
for (let t = previousLength - 1; t > rangeEnd; t--) {
events[t + insertCount] = events[t]
}

// Merge original events with insertions, writing backwards.
let writeIndex = rangeEnd + insertCount
let readIndex = rangeEnd

for (let i = insertCount - 1; i >= 0; i--) {
const position = insertPositions[i]

while (readIndex >= position) {
events[writeIndex--] = events[readIndex--]
}

events[writeIndex--] = insertEvents[i]
}
}

events[start][1]._spread = listSpread
return length
return length + insertCount
}

/**
Expand Down
Loading