book-source 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/StructuredDocument.js +60 -29
- package/lib/documentParts.js +28 -18
- package/package.json +1 -1
|
@@ -675,12 +675,13 @@ function parseOrderedListItem( str , ctx , indent ) {
|
|
|
675
675
|
var lastPart = ctx.parts[ ctx.parts.length - 1 ] ;
|
|
676
676
|
|
|
677
677
|
if ( ! lastPart || lastPart.type !== 'orderedList' ) {
|
|
678
|
-
|
|
678
|
+
lastPart = new documentParts.OrderedList( indent ) ;
|
|
679
|
+
ctx.parts.push( lastPart ) ;
|
|
679
680
|
}
|
|
680
681
|
|
|
681
682
|
stack( ctx ) ;
|
|
682
683
|
|
|
683
|
-
ctx.parts.push( new documentParts.OrderedListItem( indent , order ) ) ;
|
|
684
|
+
ctx.parts.push( new documentParts.OrderedListItem( indent , order , lastPart.autoIndex ++ ) ) ;
|
|
684
685
|
|
|
685
686
|
var blockEnd = detectBlockEnd( str , ctx.i , ctx.parent?.indent , LIST_ITEM_END_PARAMS ) ;
|
|
686
687
|
parseInlineChildren( str , ctx , blockEnd ) ;
|
|
@@ -1323,8 +1324,7 @@ function parseInlineChildrenOfParent( str , ctx , parent , blockEnd , trim = fal
|
|
|
1323
1324
|
// Try to parse non-block content
|
|
1324
1325
|
function parseInline( str , ctx , blockEnd , trim = false ) {
|
|
1325
1326
|
//console.log( "parseInline() -- remaining:" , ctx.i , str.slice( ctx.i ) ) ;
|
|
1326
|
-
var isSpace , scanEnd
|
|
1327
|
-
lastWasSpace = WHITE_SPACES.has( str[ ctx.i - 1 ] ) ;
|
|
1327
|
+
var isSpace , scanEnd ;
|
|
1328
1328
|
|
|
1329
1329
|
scanEnd = blockEnd = blockEnd ?? searchEndOfLine( str , ctx.i ) ;
|
|
1330
1330
|
|
|
@@ -1343,6 +1343,19 @@ function parseInline( str , ctx , blockEnd , trim = false ) {
|
|
|
1343
1343
|
scanEnd = last + 1 ;
|
|
1344
1344
|
}
|
|
1345
1345
|
|
|
1346
|
+
parseNestedInline( str , ctx , scanEnd , true ) ;
|
|
1347
|
+
}
|
|
1348
|
+
|
|
1349
|
+
|
|
1350
|
+
|
|
1351
|
+
function parseNestedInline( str , ctx , scanEnd , topLevel = false ) {
|
|
1352
|
+
var isSpace ,
|
|
1353
|
+
lastWasSpace = WHITE_SPACES.has( str[ ctx.i - 1 ] ) ;
|
|
1354
|
+
|
|
1355
|
+
//console.log( "parseInline() -- remaining:" , ctx.i , str.slice( ctx.i ) ) ;
|
|
1356
|
+
|
|
1357
|
+
if ( ! topLevel ) { stack( ctx ) ; }
|
|
1358
|
+
|
|
1346
1359
|
ctx.iStartOfInlineChunk = ctx.i ;
|
|
1347
1360
|
|
|
1348
1361
|
for ( ; ctx.i < scanEnd ; ctx.i ++ ) {
|
|
@@ -1387,8 +1400,10 @@ function parseInline( str , ctx , blockEnd , trim = false ) {
|
|
|
1387
1400
|
|
|
1388
1401
|
addInlineTextChunk( str , ctx ) ;
|
|
1389
1402
|
|
|
1390
|
-
ctx.i =
|
|
1403
|
+
ctx.i = scanEnd ;
|
|
1391
1404
|
if ( str[ ctx.i ] === '\n' ) { ctx.i ++ ; }
|
|
1405
|
+
|
|
1406
|
+
if ( ! topLevel ) { unstack( ctx ) ; }
|
|
1392
1407
|
}
|
|
1393
1408
|
|
|
1394
1409
|
|
|
@@ -1466,44 +1481,48 @@ function parseEscape( str , ctx ) {
|
|
|
1466
1481
|
|
|
1467
1482
|
|
|
1468
1483
|
|
|
1469
|
-
function parseEmphasisText( str , ctx ,
|
|
1484
|
+
function parseEmphasisText( str , ctx , scanEnd ) {
|
|
1470
1485
|
//console.error( "parseStyledText()" ) ;
|
|
1471
1486
|
var streak = countStreak( str , ctx.i , '*' ) ;
|
|
1472
1487
|
if ( streak > 3 ) { return ; }
|
|
1473
|
-
var end = searchSwitchCloser( str , ctx.i + streak , '*' , streak , true , false ,
|
|
1488
|
+
var end = searchSwitchCloser( str , ctx.i + streak , '*' , streak , true , false , scanEnd ) ;
|
|
1474
1489
|
if ( end < 0 ) { return ; }
|
|
1475
1490
|
|
|
1476
|
-
|
|
1491
|
+
ctx.parts.push( new documentParts.EmphasisText( streak ) ) ;
|
|
1492
|
+
|
|
1493
|
+
ctx.i += streak ;
|
|
1494
|
+
parseNestedInline( str , ctx , end + 1 - streak ) ;
|
|
1477
1495
|
|
|
1478
|
-
ctx.parts.push( new documentParts.EmphasisText( text , streak ) ) ;
|
|
1479
1496
|
ctx.i = end ;
|
|
1480
1497
|
ctx.iStartOfInlineChunk = ctx.i + 1 ;
|
|
1481
1498
|
}
|
|
1482
1499
|
|
|
1483
1500
|
|
|
1484
1501
|
|
|
1485
|
-
function parseDecoratedText( str , ctx ,
|
|
1502
|
+
function parseDecoratedText( str , ctx , scanEnd ) {
|
|
1486
1503
|
//console.error( "parseStyledText()" ) ;
|
|
1487
1504
|
var streak = countStreak( str , ctx.i , '_' ) ;
|
|
1488
1505
|
if ( streak > 2 ) { return ; }
|
|
1489
|
-
var end = searchSwitchCloser( str , ctx.i + streak , '_' , streak , true , false ,
|
|
1506
|
+
var end = searchSwitchCloser( str , ctx.i + streak , '_' , streak , true , false , scanEnd ) ;
|
|
1490
1507
|
if ( end < 0 ) { return ; }
|
|
1491
1508
|
|
|
1492
|
-
|
|
1509
|
+
ctx.parts.push( new documentParts.DecoratedText( streak ) ) ;
|
|
1510
|
+
|
|
1511
|
+
ctx.i += streak ;
|
|
1512
|
+
parseNestedInline( str , ctx , end + 1 - streak ) ;
|
|
1493
1513
|
|
|
1494
|
-
ctx.parts.push( new documentParts.DecoratedText( text , streak ) ) ;
|
|
1495
1514
|
ctx.i = end ;
|
|
1496
1515
|
ctx.iStartOfInlineChunk = ctx.i + 1 ;
|
|
1497
1516
|
}
|
|
1498
1517
|
|
|
1499
1518
|
|
|
1500
1519
|
|
|
1501
|
-
function parseCode( str , ctx ,
|
|
1520
|
+
function parseCode( str , ctx , scanEnd ) {
|
|
1502
1521
|
//console.error( "parseStyledText()" ) ;
|
|
1503
1522
|
var streak = countStreak( str , ctx.i , '`' ) ;
|
|
1504
1523
|
// Markdown supports inline code inside two pairs of backquote, to allow backquote in code, hence streak can be 2.
|
|
1505
1524
|
if ( streak > 2 ) { return ; }
|
|
1506
|
-
var end = searchSwitchCloser( str , ctx.i + streak , '`' , streak , false , false ,
|
|
1525
|
+
var end = searchSwitchCloser( str , ctx.i + streak , '`' , streak , false , false , scanEnd ) ;
|
|
1507
1526
|
if ( end < 0 ) { return ; }
|
|
1508
1527
|
|
|
1509
1528
|
var sliceStart = ctx.i + streak ,
|
|
@@ -1525,27 +1544,39 @@ const STYLE_DATA_MARK = {
|
|
|
1525
1544
|
text: true , href: true , style: true , extra: false
|
|
1526
1545
|
} ;
|
|
1527
1546
|
|
|
1528
|
-
function parseStyledText( str , ctx ,
|
|
1547
|
+
function parseStyledText( str , ctx , scanEnd ) {
|
|
1529
1548
|
//console.error( "parseStyledText()" ) ;
|
|
1530
|
-
var end = searchCloser( str , ctx.i + 1 , '[' , ']' , false ,
|
|
1549
|
+
var end = searchCloser( str , ctx.i + 1 , '[' , ']' , false , scanEnd ) ;
|
|
1531
1550
|
if ( end < 0 ) { return ; }
|
|
1532
1551
|
|
|
1533
|
-
var text = str.slice( ctx.i + 1 , end ) ;
|
|
1552
|
+
//var text = str.slice( ctx.i + 1 , end ) ;
|
|
1534
1553
|
|
|
1554
|
+
var start = ctx.i + 1 ;
|
|
1535
1555
|
ctx.i = end ;
|
|
1536
|
-
var data = parseDataMark( str , ctx , STYLE_DATA_MARK ,
|
|
1556
|
+
var data = parseDataMark( str , ctx , STYLE_DATA_MARK , scanEnd ) ;
|
|
1537
1557
|
if ( ! data ) { return ; }
|
|
1538
1558
|
|
|
1559
|
+
var fullMarkupEnd = ctx.i ;
|
|
1560
|
+
|
|
1539
1561
|
var href = data.href?.[ 0 ] ,
|
|
1540
1562
|
style = data.style?.[ 0 ] ,
|
|
1541
1563
|
title = data.text?.[ 0 ] ;
|
|
1542
1564
|
|
|
1543
1565
|
if ( href ) {
|
|
1544
|
-
ctx.parts.push( new documentParts.Link(
|
|
1566
|
+
ctx.parts.push( new documentParts.Link( href , style , title ) ) ;
|
|
1545
1567
|
}
|
|
1546
1568
|
else if ( style || title ) {
|
|
1547
|
-
ctx.parts.push( new documentParts.StyledText(
|
|
1569
|
+
ctx.parts.push( new documentParts.StyledText( style , title ) ) ;
|
|
1548
1570
|
}
|
|
1571
|
+
else {
|
|
1572
|
+
return ;
|
|
1573
|
+
}
|
|
1574
|
+
|
|
1575
|
+
ctx.i = start ;
|
|
1576
|
+
parseNestedInline( str , ctx , end ) ;
|
|
1577
|
+
|
|
1578
|
+
ctx.i = fullMarkupEnd ;
|
|
1579
|
+
ctx.iStartOfInlineChunk = ctx.i + 1 ;
|
|
1549
1580
|
}
|
|
1550
1581
|
|
|
1551
1582
|
|
|
@@ -1554,16 +1585,16 @@ const IMAGE_DATA_MARK = {
|
|
|
1554
1585
|
text: true , href: true , style: false , extra: false
|
|
1555
1586
|
} ;
|
|
1556
1587
|
|
|
1557
|
-
function parseImage( str , ctx ,
|
|
1588
|
+
function parseImage( str , ctx , scanEnd ) {
|
|
1558
1589
|
//console.error( "parseStyledText()" ) ;
|
|
1559
|
-
var end = searchCloser( str , ctx.i + 2 , '[' , ']' , false ,
|
|
1590
|
+
var end = searchCloser( str , ctx.i + 2 , '[' , ']' , false , scanEnd ) ;
|
|
1560
1591
|
if ( end < 0 ) { return ; }
|
|
1561
1592
|
|
|
1562
1593
|
var text = str.slice( ctx.i + 2 , end ) ;
|
|
1563
1594
|
|
|
1564
1595
|
ctx.i = end ;
|
|
1565
1596
|
ctx.iStartOfInlineChunk = ctx.i + 1 ;
|
|
1566
|
-
var data = parseDataMark( str , ctx , IMAGE_DATA_MARK ,
|
|
1597
|
+
var data = parseDataMark( str , ctx , IMAGE_DATA_MARK , scanEnd ) ;
|
|
1567
1598
|
if ( ! data ) { return ; }
|
|
1568
1599
|
|
|
1569
1600
|
var href = data.href?.[ 0 ] ;
|
|
@@ -1578,13 +1609,13 @@ function parseImage( str , ctx , blockEnd ) {
|
|
|
1578
1609
|
|
|
1579
1610
|
|
|
1580
1611
|
|
|
1581
|
-
function parseDataMark( str , ctx , allow ,
|
|
1612
|
+
function parseDataMark( str , ctx , allow , scanEnd , forTextElement = true ) {
|
|
1582
1613
|
var end ,
|
|
1583
1614
|
data = {} ;
|
|
1584
1615
|
|
|
1585
1616
|
for ( ;; ) {
|
|
1586
1617
|
if ( str[ ctx.i + 1 ] === '[' && allow.text ) {
|
|
1587
|
-
end = searchCloser( str , ctx.i + 2 , '[' , ']' , false ,
|
|
1618
|
+
end = searchCloser( str , ctx.i + 2 , '[' , ']' , false , scanEnd ) ;
|
|
1588
1619
|
if ( end < 0 ) { return ; }
|
|
1589
1620
|
if ( ! data.text ) { data.text = [] ; }
|
|
1590
1621
|
data.text.push( str.slice( ctx.i + 2 , end ) ) ;
|
|
@@ -1592,7 +1623,7 @@ function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
|
|
|
1592
1623
|
ctx.iStartOfInlineChunk = ctx.i + 1 ;
|
|
1593
1624
|
}
|
|
1594
1625
|
else if ( str[ ctx.i + 1 ] === '(' && allow.href ) {
|
|
1595
|
-
end = searchCloser( str , ctx.i + 2 , '(' , ')' , true ,
|
|
1626
|
+
end = searchCloser( str , ctx.i + 2 , '(' , ')' , true , scanEnd ) ;
|
|
1596
1627
|
if ( end < 0 ) { return ; }
|
|
1597
1628
|
if ( ! data.href ) { data.href = [] ; }
|
|
1598
1629
|
data.href.push( str.slice( ctx.i + 2 , end ) ) ;
|
|
@@ -1600,7 +1631,7 @@ function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
|
|
|
1600
1631
|
ctx.iStartOfInlineChunk = ctx.i + 1 ;
|
|
1601
1632
|
}
|
|
1602
1633
|
else if ( str[ ctx.i + 1 ] === '<' && allow.style ) {
|
|
1603
|
-
end = searchCloser( str , ctx.i + 2 , '<' , '>' , true ,
|
|
1634
|
+
end = searchCloser( str , ctx.i + 2 , '<' , '>' , true , scanEnd ) ;
|
|
1604
1635
|
if ( end < 0 ) { return ; }
|
|
1605
1636
|
if ( ! data.style ) { data.style = [] ; }
|
|
1606
1637
|
//data.style.push( str.slice( ctx.i + 2 , end ) ) ;
|
|
@@ -1612,7 +1643,7 @@ function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
|
|
|
1612
1643
|
}
|
|
1613
1644
|
/*
|
|
1614
1645
|
else if ( str[ ctx.i + 1 ] === '{' && allow.extra ) {
|
|
1615
|
-
end = searchCloser( str , ctx.i + 2 , '{' , '}' , false ,
|
|
1646
|
+
end = searchCloser( str , ctx.i + 2 , '{' , '}' , false , scanEnd ) ;
|
|
1616
1647
|
if ( end < 0 ) { return ; }
|
|
1617
1648
|
if ( ! data.extra ) { data.extra = [] ; }
|
|
1618
1649
|
data.extra.push( str.slice( ctx.i + 2 , end ) ) ;
|
package/lib/documentParts.js
CHANGED
|
@@ -33,15 +33,13 @@ module.exports = documentParts ;
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
|
|
36
|
-
function Part() {
|
|
37
|
-
}
|
|
36
|
+
function Part() {}
|
|
38
37
|
|
|
39
38
|
documentParts.Part = Part ;
|
|
40
39
|
|
|
41
40
|
|
|
42
41
|
|
|
43
|
-
function InlinePart() {
|
|
44
|
-
}
|
|
42
|
+
function InlinePart() {}
|
|
45
43
|
|
|
46
44
|
InlinePart.prototype = Object.create( Part.prototype ) ;
|
|
47
45
|
InlinePart.prototype.constructor = InlinePart ;
|
|
@@ -49,6 +47,16 @@ documentParts.InlinePart = InlinePart ;
|
|
|
49
47
|
|
|
50
48
|
|
|
51
49
|
|
|
50
|
+
function InlineContainerPart() {
|
|
51
|
+
this.parts = [] ;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
InlineContainerPart.prototype = Object.create( Part.prototype ) ;
|
|
55
|
+
InlineContainerPart.prototype.constructor = InlineContainerPart ;
|
|
56
|
+
documentParts.InlineContainerPart = InlineContainerPart ;
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
52
60
|
function InlineTextPart( text ) {
|
|
53
61
|
this.text = text ;
|
|
54
62
|
}
|
|
@@ -82,26 +90,26 @@ documentParts.Text = Text ;
|
|
|
82
90
|
|
|
83
91
|
|
|
84
92
|
|
|
85
|
-
function EmphasisText(
|
|
93
|
+
function EmphasisText( level ) {
|
|
86
94
|
this.type = 'emphasisText' ;
|
|
87
95
|
this.level = level ;
|
|
88
|
-
|
|
96
|
+
InlineContainerPart.call( this ) ;
|
|
89
97
|
}
|
|
90
98
|
|
|
91
|
-
EmphasisText.prototype = Object.create(
|
|
99
|
+
EmphasisText.prototype = Object.create( InlineContainerPart.prototype ) ;
|
|
92
100
|
EmphasisText.prototype.constructor = EmphasisText ;
|
|
93
101
|
documentParts.EmphasisText = EmphasisText ;
|
|
94
102
|
|
|
95
103
|
|
|
96
104
|
|
|
97
|
-
function DecoratedText(
|
|
105
|
+
function DecoratedText( level ) {
|
|
98
106
|
this.type = 'decoratedText' ;
|
|
99
107
|
this.level = level ;
|
|
100
108
|
this.underline = true ;
|
|
101
|
-
|
|
109
|
+
InlineContainerPart.call( this ) ;
|
|
102
110
|
}
|
|
103
111
|
|
|
104
|
-
DecoratedText.prototype = Object.create(
|
|
112
|
+
DecoratedText.prototype = Object.create( InlineContainerPart.prototype ) ;
|
|
105
113
|
DecoratedText.prototype.constructor = DecoratedText ;
|
|
106
114
|
documentParts.DecoratedText = DecoratedText ;
|
|
107
115
|
|
|
@@ -118,28 +126,28 @@ documentParts.Code = Code ;
|
|
|
118
126
|
|
|
119
127
|
|
|
120
128
|
|
|
121
|
-
function Link(
|
|
129
|
+
function Link( href , style , title ) {
|
|
122
130
|
this.type = 'link' ;
|
|
123
131
|
this.href = href ;
|
|
124
132
|
this.style = style || undefined ;
|
|
125
|
-
|
|
133
|
+
InlineContainerPart.call( this ) ;
|
|
126
134
|
this.title = title || undefined ;
|
|
127
135
|
}
|
|
128
136
|
|
|
129
|
-
Link.prototype = Object.create(
|
|
137
|
+
Link.prototype = Object.create( InlineContainerPart.prototype ) ;
|
|
130
138
|
Link.prototype.constructor = Link ;
|
|
131
139
|
documentParts.Link = Link ;
|
|
132
140
|
|
|
133
141
|
|
|
134
142
|
|
|
135
|
-
function StyledText(
|
|
143
|
+
function StyledText( style , title ) {
|
|
136
144
|
this.type = 'styledText' ;
|
|
137
145
|
this.style = style || undefined ;
|
|
138
|
-
|
|
146
|
+
InlineContainerPart.call( this ) ;
|
|
139
147
|
this.title = title || undefined ;
|
|
140
148
|
}
|
|
141
149
|
|
|
142
|
-
StyledText.prototype = Object.create(
|
|
150
|
+
StyledText.prototype = Object.create( InlineContainerPart.prototype ) ;
|
|
143
151
|
StyledText.prototype.constructor = StyledText ;
|
|
144
152
|
documentParts.StyledText = StyledText ;
|
|
145
153
|
|
|
@@ -247,6 +255,7 @@ documentParts.ListItem = ListItem ;
|
|
|
247
255
|
function OrderedList( indent ) {
|
|
248
256
|
this.type = 'orderedList' ;
|
|
249
257
|
this.indent = indent ;
|
|
258
|
+
this.autoIndex = 1 ;
|
|
250
259
|
BlockPart.call( this ) ;
|
|
251
260
|
}
|
|
252
261
|
|
|
@@ -256,10 +265,11 @@ documentParts.OrderedList = OrderedList ;
|
|
|
256
265
|
|
|
257
266
|
|
|
258
267
|
|
|
259
|
-
function OrderedListItem( indent , order ) {
|
|
268
|
+
function OrderedListItem( indent , order , index ) {
|
|
260
269
|
this.type = 'orderedListItem' ;
|
|
261
270
|
this.indent = indent ;
|
|
262
|
-
this.order = order ;
|
|
271
|
+
this.order = order ; // User order value
|
|
272
|
+
this.index = index ; // Real index, starting at 1, and auto-incrementing
|
|
263
273
|
BlockPart.call( this ) ;
|
|
264
274
|
}
|
|
265
275
|
|