Skip to content
This repository has been archived by the owner on Aug 22, 2019. It is now read-only.

Support offset when numbering #1

Merged
merged 1 commit into from
Jul 24, 2017
Merged
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
4 changes: 2 additions & 2 deletions lib/numbering.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ function getNumberingRanges(str) {
* @return {String}
*/
function replaceNumberingRanges(str, ranges, value) {
const replaced = replaceRanges(str, ranges, token => {
let _value = String(value);
const replaced = replaceRanges(str, ranges, (token, offset) => {
let _value = String(value + offset);
// pad values for multiple numbering tokens, e.g. 3 for $$$ becomes 003
while (_value.length < token.length) {
_value = '0' + _value;
Expand Down
14 changes: 12 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,19 @@ export function replaceRanges(str, ranges, value) {
for (let i = ranges.length - 1; i >= 0; i--) {
const r = ranges[i];

let offset = 0;
let offsetLength = 0;
if (str.substr(r[0] + r[1], 1) === '@'){
const matches = str.substr(r[0] + r[1] + 1).match(/^(\d+)/);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s also another option to reverse direction of numbering: https://docs.emmet.io/abbreviations/syntax/#changing-numbering-base-and-direction

But will approve for now

if (matches) {
offsetLength = matches[1].length + 1;
offset = parseInt(matches[1]) - 1;
}
}

str = str.substring(0, r[0])
+ (typeof value === 'function' ? value(str.substr(r[0], r[1])) : value)
+ str.substring(r[0] + r[1]);
+ (typeof value === 'function' ? value(str.substr(r[0], r[1]), offset) : value)
+ str.substring(r[0] + r[1] + offsetLength);
}

return str;
Expand Down
5 changes: 5 additions & 0 deletions test/numbering.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ describe('Item numbering', () => {
const tree = numbering(parse('[!foo="bar$"]*2'));
assert(tree.firstChild.getAttribute('foo').options.implied);
});

it('add offset to numbering', () => {
assert.equal(expand('span.item$@3*2'), '<span class="item3"></span><span class="item4"></span>');
assert.equal(expand('span.item$$@3*2'), '<span class="item03"></span><span class="item04"></span>');
});
});