This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 204
Footnote support #441
Merged
Merged
Footnote support #441
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
75187c1
Footnote support
a91fa8e
fixup rebase
a085b9b
fixup ci
ac1cd09
remove unused
903e7f7
rebase fix
df5ca74
fix review
e7679d2
footnote field & syntax excuding
a8a81fc
refine comments
6f3c6b0
fix ci format error
e92c221
fix review IV
90dbd23
fix review V
19aebf3
add a period
73259ec
add link note
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import '../ast.dart' show Element, Node; | ||
import '../block_parser.dart' show BlockParser; | ||
import '../line.dart'; | ||
import '../patterns.dart' show dummyPattern, emptyPattern, footnotePattern; | ||
import 'block_syntax.dart' show BlockSyntax; | ||
|
||
/// The spec of GFM about footnotes is [missing](https://github.com/github/cmark-gfm/issues/283#issuecomment-1378868725). | ||
/// For online source code of cmark-gfm, see [master@c32ef78](https://github.com/github/cmark-gfm/blob/c32ef78/src/blocks.c#L1212). | ||
/// A Rust implementation is also [available](https://github.com/wooorm/markdown-rs/blob/2498e31eecead798efc649502bbf5f86feaa94be/src/construct/gfm_footnote_definition.rs). | ||
/// Footnote definition could contain multiple line-children and children could | ||
/// be separated by one empty line. | ||
/// Its first child-line would be the remaining part of the first line after | ||
/// taking definition leading, combining with other child lines parsed by | ||
/// [parseChildLines], is fed into [BlockParser]. | ||
class FootnoteDefSyntax extends BlockSyntax { | ||
const FootnoteDefSyntax(); | ||
|
||
@override | ||
RegExp get pattern => footnotePattern; | ||
|
||
@override | ||
Node? parse(BlockParser parser) { | ||
final current = parser.current.content; | ||
final match = pattern.firstMatch(current)!; | ||
final label = match[2]!; | ||
final refs = parser.document.footnoteReferences; | ||
refs[label] = 0; | ||
|
||
final id = Uri.encodeComponent(label); | ||
parser.advance(); | ||
final lines = [ | ||
Line(current.substring(match[0]!.length)), | ||
...parseChildLines(parser), | ||
]; | ||
final children = BlockParser(lines, parser.document).parseLines(); | ||
return Element('li', children) | ||
..attributes['id'] = 'fn-$id' | ||
..footnoteLabel = label; | ||
} | ||
|
||
@override | ||
List<Line> parseChildLines(BlockParser parser) { | ||
final children = <String>[]; | ||
// As one empty line should not split footnote definition, use this flag. | ||
var shouldBeBlock = false; | ||
late final syntaxList = parser.blockSyntaxes | ||
.where((s) => !_excludingPattern.contains(s.pattern)); | ||
|
||
// Every line is footnote's children util two blank lines or a block. | ||
while (!parser.isDone) { | ||
final line = parser.current.content; | ||
if (line.trim().isEmpty) { | ||
children.add(line); | ||
parser.advance(); | ||
shouldBeBlock = true; | ||
continue; | ||
} else if (line.startsWith(' ')) { | ||
children.add(line.substring(4)); | ||
parser.advance(); | ||
shouldBeBlock = false; | ||
} else if (shouldBeBlock || _isBlock(syntaxList, line)) { | ||
break; | ||
} else { | ||
children.add(line); | ||
parser.advance(); | ||
} | ||
} | ||
return children.map(Line.new).toList(growable: false); | ||
} | ||
|
||
/// Patterns that would be used to decide if one line is a block. | ||
static final _excludingPattern = { | ||
lindeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
emptyPattern, | ||
dummyPattern, | ||
}; | ||
|
||
/// Whether this line is one kind of block, if true footnotes block should end. | ||
static bool _isBlock(Iterable<BlockSyntax> syntaxList, String line) { | ||
lindeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return syntaxList.any((s) => s.pattern.hasMatch(line)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import '../ast.dart' show Element, Node, Text; | ||
import '../charcode.dart'; | ||
import 'link_syntax.dart' show LinkContext; | ||
|
||
/// The spec of GFM about footnotes is [missing](https://github.com/github/cmark-gfm/issues/283#issuecomment-1378868725). | ||
/// For source code of cmark-gfm, See [noMatch] label of [handle_close_bracket] function in [master@c32ef78](https://github.com/github/cmark-gfm/blob/c32ef78/src/inlines.c#L1236). | ||
/// A Rust implementation is also [available](https://github.com/wooorm/markdown-rs/blob/2498e31eecead798efc649502bbf5f86feaa94be/src/construct/gfm_label_start_footnote.rs). | ||
/// Footnote shares the same syntax with [LinkSyntax], but goes a different branch of handling close bracket. | ||
class FootnoteRefSyntax { | ||
lindeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static String? _footnoteLabel(String key) { | ||
if (key.isEmpty || key.codeUnitAt(0) != $caret) { | ||
return null; | ||
} | ||
key = key.substring(1).trim().toLowerCase(); | ||
if (key.isEmpty) { | ||
return null; | ||
} | ||
return key; | ||
} | ||
|
||
static Iterable<Node>? tryCreateFootnoteLink( | ||
LinkContext context, | ||
String text, { | ||
bool? secondary, | ||
}) { | ||
secondary ??= false; | ||
final parser = context.parser; | ||
final key = _footnoteLabel(text); | ||
final refs = parser.document.footnoteReferences; | ||
// `label` is what footnoteReferences stored, it is case sensitive. | ||
final label = | ||
refs.keys.firstWhere((k) => k.toLowerCase() == key, orElse: () => ''); | ||
// `count != null` means footnote was valid. | ||
var count = refs[label]; | ||
// And then check if footnote was matched. | ||
if (key == null || count == null) { | ||
return null; | ||
} | ||
final result = <Node>[]; | ||
// There are 4 cases here: ![^...], [^...], ![...][^...], [...][^...] | ||
lindeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (context.opener.char == $exclamation) { | ||
result.add(Text('!')); | ||
} | ||
refs[label] = ++count; | ||
final labels = parser.document.footnoteLabels; | ||
var pos = labels.indexOf(key); | ||
if (pos < 0) { | ||
pos = labels.length; | ||
labels.add(key); | ||
} | ||
|
||
// `children` are text segments after '[^' before ']'. | ||
final children = context.getChildren(); | ||
if (secondary) { | ||
result.add(Text('[')); | ||
result.addAll(children); | ||
result.add(Text(']')); | ||
} | ||
final id = Uri.encodeComponent(label); | ||
final suffix = count > 1 ? '-$count' : ''; | ||
final link = Element('a', [Text('${pos + 1}')]) | ||
// Ignore GitHub's attribute: <data-footnote-ref>. | ||
..attributes['href'] = '#fn-$id' | ||
..attributes['id'] = 'fnref-$id$suffix'; | ||
final sup = Element('sup', [link])..attributes['class'] = 'footnote-ref'; | ||
result.add(sup); | ||
return result; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.