blob: c491a50a8bbb3cb727b2b8cc39f6d491105f6795 [file] [log] [blame]
// Copyright 2017 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:io';
import 'package:markdown/markdown.dart';
import 'package:meta/meta.dart';
/// Scrapes links in a markdown document.
class LinkScraper {
/// Extracts links from the given [file].
Iterable<String> scrape(String file) {
return scrapeLines(File(file).readAsLinesSync());
/// Extracts links from the given list of [lines].
Iterable<String> scrapeLines(List<String> lines) {
final List<Node> nodes = Document().parseLines(lines);
final _Visitor visitor = _Visitor();
for (Node node in nodes) {
return visitor.links;
class _Visitor implements NodeVisitor {
static const String _key = 'href';
final Set<String> links = <String>{};
bool visitElementBefore(Element element) {
if (element.attributes.containsKey(_key)) {
return true;
void visitElementAfter(Element element) {}
void visitText(Text text) {}