book-source 0.2.4 → 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.
@@ -1324,8 +1324,7 @@ function parseInlineChildrenOfParent( str , ctx , parent , blockEnd , trim = fal
1324
1324
  // Try to parse non-block content
1325
1325
  function parseInline( str , ctx , blockEnd , trim = false ) {
1326
1326
  //console.log( "parseInline() -- remaining:" , ctx.i , str.slice( ctx.i ) ) ;
1327
- var isSpace , scanEnd ,
1328
- lastWasSpace = WHITE_SPACES.has( str[ ctx.i - 1 ] ) ;
1327
+ var isSpace , scanEnd ;
1329
1328
 
1330
1329
  scanEnd = blockEnd = blockEnd ?? searchEndOfLine( str , ctx.i ) ;
1331
1330
 
@@ -1344,6 +1343,19 @@ function parseInline( str , ctx , blockEnd , trim = false ) {
1344
1343
  scanEnd = last + 1 ;
1345
1344
  }
1346
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
+
1347
1359
  ctx.iStartOfInlineChunk = ctx.i ;
1348
1360
 
1349
1361
  for ( ; ctx.i < scanEnd ; ctx.i ++ ) {
@@ -1388,8 +1400,10 @@ function parseInline( str , ctx , blockEnd , trim = false ) {
1388
1400
 
1389
1401
  addInlineTextChunk( str , ctx ) ;
1390
1402
 
1391
- ctx.i = blockEnd ;
1403
+ ctx.i = scanEnd ;
1392
1404
  if ( str[ ctx.i ] === '\n' ) { ctx.i ++ ; }
1405
+
1406
+ if ( ! topLevel ) { unstack( ctx ) ; }
1393
1407
  }
1394
1408
 
1395
1409
 
@@ -1467,44 +1481,48 @@ function parseEscape( str , ctx ) {
1467
1481
 
1468
1482
 
1469
1483
 
1470
- function parseEmphasisText( str , ctx , blockEnd ) {
1484
+ function parseEmphasisText( str , ctx , scanEnd ) {
1471
1485
  //console.error( "parseStyledText()" ) ;
1472
1486
  var streak = countStreak( str , ctx.i , '*' ) ;
1473
1487
  if ( streak > 3 ) { return ; }
1474
- var end = searchSwitchCloser( str , ctx.i + streak , '*' , streak , true , false , blockEnd ) ;
1488
+ var end = searchSwitchCloser( str , ctx.i + streak , '*' , streak , true , false , scanEnd ) ;
1475
1489
  if ( end < 0 ) { return ; }
1476
1490
 
1477
- var text = str.slice( ctx.i + streak , end + 1 - streak ) ;
1491
+ ctx.parts.push( new documentParts.EmphasisText( streak ) ) ;
1492
+
1493
+ ctx.i += streak ;
1494
+ parseNestedInline( str , ctx , end + 1 - streak ) ;
1478
1495
 
1479
- ctx.parts.push( new documentParts.EmphasisText( text , streak ) ) ;
1480
1496
  ctx.i = end ;
1481
1497
  ctx.iStartOfInlineChunk = ctx.i + 1 ;
1482
1498
  }
1483
1499
 
1484
1500
 
1485
1501
 
1486
- function parseDecoratedText( str , ctx , blockEnd ) {
1502
+ function parseDecoratedText( str , ctx , scanEnd ) {
1487
1503
  //console.error( "parseStyledText()" ) ;
1488
1504
  var streak = countStreak( str , ctx.i , '_' ) ;
1489
1505
  if ( streak > 2 ) { return ; }
1490
- var end = searchSwitchCloser( str , ctx.i + streak , '_' , streak , true , false , blockEnd ) ;
1506
+ var end = searchSwitchCloser( str , ctx.i + streak , '_' , streak , true , false , scanEnd ) ;
1491
1507
  if ( end < 0 ) { return ; }
1492
1508
 
1493
- var text = str.slice( ctx.i + streak , end + 1 - streak ) ;
1509
+ ctx.parts.push( new documentParts.DecoratedText( streak ) ) ;
1510
+
1511
+ ctx.i += streak ;
1512
+ parseNestedInline( str , ctx , end + 1 - streak ) ;
1494
1513
 
1495
- ctx.parts.push( new documentParts.DecoratedText( text , streak ) ) ;
1496
1514
  ctx.i = end ;
1497
1515
  ctx.iStartOfInlineChunk = ctx.i + 1 ;
1498
1516
  }
1499
1517
 
1500
1518
 
1501
1519
 
1502
- function parseCode( str , ctx , blockEnd ) {
1520
+ function parseCode( str , ctx , scanEnd ) {
1503
1521
  //console.error( "parseStyledText()" ) ;
1504
1522
  var streak = countStreak( str , ctx.i , '`' ) ;
1505
1523
  // Markdown supports inline code inside two pairs of backquote, to allow backquote in code, hence streak can be 2.
1506
1524
  if ( streak > 2 ) { return ; }
1507
- var end = searchSwitchCloser( str , ctx.i + streak , '`' , streak , false , false , blockEnd ) ;
1525
+ var end = searchSwitchCloser( str , ctx.i + streak , '`' , streak , false , false , scanEnd ) ;
1508
1526
  if ( end < 0 ) { return ; }
1509
1527
 
1510
1528
  var sliceStart = ctx.i + streak ,
@@ -1526,27 +1544,39 @@ const STYLE_DATA_MARK = {
1526
1544
  text: true , href: true , style: true , extra: false
1527
1545
  } ;
1528
1546
 
1529
- function parseStyledText( str , ctx , blockEnd ) {
1547
+ function parseStyledText( str , ctx , scanEnd ) {
1530
1548
  //console.error( "parseStyledText()" ) ;
1531
- var end = searchCloser( str , ctx.i + 1 , '[' , ']' , false , blockEnd ) ;
1549
+ var end = searchCloser( str , ctx.i + 1 , '[' , ']' , false , scanEnd ) ;
1532
1550
  if ( end < 0 ) { return ; }
1533
1551
 
1534
- var text = str.slice( ctx.i + 1 , end ) ;
1552
+ //var text = str.slice( ctx.i + 1 , end ) ;
1535
1553
 
1554
+ var start = ctx.i + 1 ;
1536
1555
  ctx.i = end ;
1537
- var data = parseDataMark( str , ctx , STYLE_DATA_MARK , blockEnd ) ;
1556
+ var data = parseDataMark( str , ctx , STYLE_DATA_MARK , scanEnd ) ;
1538
1557
  if ( ! data ) { return ; }
1539
1558
 
1559
+ var fullMarkupEnd = ctx.i ;
1560
+
1540
1561
  var href = data.href?.[ 0 ] ,
1541
1562
  style = data.style?.[ 0 ] ,
1542
1563
  title = data.text?.[ 0 ] ;
1543
1564
 
1544
1565
  if ( href ) {
1545
- ctx.parts.push( new documentParts.Link( text , href , style , title ) ) ;
1566
+ ctx.parts.push( new documentParts.Link( href , style , title ) ) ;
1546
1567
  }
1547
1568
  else if ( style || title ) {
1548
- ctx.parts.push( new documentParts.StyledText( text , style , title ) ) ;
1569
+ ctx.parts.push( new documentParts.StyledText( style , title ) ) ;
1549
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 ;
1550
1580
  }
1551
1581
 
1552
1582
 
@@ -1555,16 +1585,16 @@ const IMAGE_DATA_MARK = {
1555
1585
  text: true , href: true , style: false , extra: false
1556
1586
  } ;
1557
1587
 
1558
- function parseImage( str , ctx , blockEnd ) {
1588
+ function parseImage( str , ctx , scanEnd ) {
1559
1589
  //console.error( "parseStyledText()" ) ;
1560
- var end = searchCloser( str , ctx.i + 2 , '[' , ']' , false , blockEnd ) ;
1590
+ var end = searchCloser( str , ctx.i + 2 , '[' , ']' , false , scanEnd ) ;
1561
1591
  if ( end < 0 ) { return ; }
1562
1592
 
1563
1593
  var text = str.slice( ctx.i + 2 , end ) ;
1564
1594
 
1565
1595
  ctx.i = end ;
1566
1596
  ctx.iStartOfInlineChunk = ctx.i + 1 ;
1567
- var data = parseDataMark( str , ctx , IMAGE_DATA_MARK , blockEnd ) ;
1597
+ var data = parseDataMark( str , ctx , IMAGE_DATA_MARK , scanEnd ) ;
1568
1598
  if ( ! data ) { return ; }
1569
1599
 
1570
1600
  var href = data.href?.[ 0 ] ;
@@ -1579,13 +1609,13 @@ function parseImage( str , ctx , blockEnd ) {
1579
1609
 
1580
1610
 
1581
1611
 
1582
- function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
1612
+ function parseDataMark( str , ctx , allow , scanEnd , forTextElement = true ) {
1583
1613
  var end ,
1584
1614
  data = {} ;
1585
1615
 
1586
1616
  for ( ;; ) {
1587
1617
  if ( str[ ctx.i + 1 ] === '[' && allow.text ) {
1588
- end = searchCloser( str , ctx.i + 2 , '[' , ']' , false , blockEnd ) ;
1618
+ end = searchCloser( str , ctx.i + 2 , '[' , ']' , false , scanEnd ) ;
1589
1619
  if ( end < 0 ) { return ; }
1590
1620
  if ( ! data.text ) { data.text = [] ; }
1591
1621
  data.text.push( str.slice( ctx.i + 2 , end ) ) ;
@@ -1593,7 +1623,7 @@ function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
1593
1623
  ctx.iStartOfInlineChunk = ctx.i + 1 ;
1594
1624
  }
1595
1625
  else if ( str[ ctx.i + 1 ] === '(' && allow.href ) {
1596
- end = searchCloser( str , ctx.i + 2 , '(' , ')' , true , blockEnd ) ;
1626
+ end = searchCloser( str , ctx.i + 2 , '(' , ')' , true , scanEnd ) ;
1597
1627
  if ( end < 0 ) { return ; }
1598
1628
  if ( ! data.href ) { data.href = [] ; }
1599
1629
  data.href.push( str.slice( ctx.i + 2 , end ) ) ;
@@ -1601,7 +1631,7 @@ function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
1601
1631
  ctx.iStartOfInlineChunk = ctx.i + 1 ;
1602
1632
  }
1603
1633
  else if ( str[ ctx.i + 1 ] === '<' && allow.style ) {
1604
- end = searchCloser( str , ctx.i + 2 , '<' , '>' , true , blockEnd ) ;
1634
+ end = searchCloser( str , ctx.i + 2 , '<' , '>' , true , scanEnd ) ;
1605
1635
  if ( end < 0 ) { return ; }
1606
1636
  if ( ! data.style ) { data.style = [] ; }
1607
1637
  //data.style.push( str.slice( ctx.i + 2 , end ) ) ;
@@ -1613,7 +1643,7 @@ function parseDataMark( str , ctx , allow , blockEnd , forTextElement = true ) {
1613
1643
  }
1614
1644
  /*
1615
1645
  else if ( str[ ctx.i + 1 ] === '{' && allow.extra ) {
1616
- end = searchCloser( str , ctx.i + 2 , '{' , '}' , false , blockEnd ) ;
1646
+ end = searchCloser( str , ctx.i + 2 , '{' , '}' , false , scanEnd ) ;
1617
1647
  if ( end < 0 ) { return ; }
1618
1648
  if ( ! data.extra ) { data.extra = [] ; }
1619
1649
  data.extra.push( str.slice( ctx.i + 2 , end ) ) ;
@@ -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( text , level ) {
93
+ function EmphasisText( level ) {
86
94
  this.type = 'emphasisText' ;
87
95
  this.level = level ;
88
- InlineTextPart.call( this , text ) ;
96
+ InlineContainerPart.call( this ) ;
89
97
  }
90
98
 
91
- EmphasisText.prototype = Object.create( InlineTextPart.prototype ) ;
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( text , level ) {
105
+ function DecoratedText( level ) {
98
106
  this.type = 'decoratedText' ;
99
107
  this.level = level ;
100
108
  this.underline = true ;
101
- InlineTextPart.call( this , text ) ;
109
+ InlineContainerPart.call( this ) ;
102
110
  }
103
111
 
104
- DecoratedText.prototype = Object.create( InlineTextPart.prototype ) ;
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( text , href , style , title ) {
129
+ function Link( href , style , title ) {
122
130
  this.type = 'link' ;
123
131
  this.href = href ;
124
132
  this.style = style || undefined ;
125
- InlineTextPart.call( this , text ) ;
133
+ InlineContainerPart.call( this ) ;
126
134
  this.title = title || undefined ;
127
135
  }
128
136
 
129
- Link.prototype = Object.create( InlineTextPart.prototype ) ;
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( text , style , title ) {
143
+ function StyledText( style , title ) {
136
144
  this.type = 'styledText' ;
137
145
  this.style = style || undefined ;
138
- InlineTextPart.call( this , text ) ;
146
+ InlineContainerPart.call( this ) ;
139
147
  this.title = title || undefined ;
140
148
  }
141
149
 
142
- StyledText.prototype = Object.create( InlineTextPart.prototype ) ;
150
+ StyledText.prototype = Object.create( InlineContainerPart.prototype ) ;
143
151
  StyledText.prototype.constructor = StyledText ;
144
152
  documentParts.StyledText = StyledText ;
145
153
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "book-source",
3
- "version": "0.2.4",
3
+ "version": "0.3.0",
4
4
  "description": "A lightweight markup language, inspired by Markdown.",
5
5
  "main": "lib/book-source.js",
6
6
  "directories": {