findNextFrontMatterSlide function

List<SlideData> findNextFrontMatterSlide(
  1. String value, {
  2. List<SlideData>? slides,
  3. bool firstRun = false,
})

Implementation

List<SlideData> findNextFrontMatterSlide(
  String value, {
  List<SlideData>? slides,
  bool firstRun = false,
}) {
  slides ??= <SlideData>[];
  // Get the index of the closing delimiter.
  String currentDelimiter;

  if (firstRun) {
    currentDelimiter = delimiter;
  } else {
    currentDelimiter = delimeterLineBreak;
  }

  final closeIndex = value.indexOf(delimeterLineBreak, currentDelimiter.length);

  // Get the raw front matter block between the opening and closing delimiters.
  final frontMatter = value.substring(currentDelimiter.length, closeIndex);

  // Parse the front matter as YAML.
  final slideOptions = loadYaml(frontMatter) as YamlMap?;
  // The content begins after the closing delimiter index.
  final slideContent =
      value.substring(closeIndex + (currentDelimiter.length + 1));

  final nextIndex = slideContent.indexOf(delimeterLineBreak);

  final lastSlide = nextIndex == -1;

  String getCurrentContent() {
    if (lastSlide) {
      return slideContent;
    } else {
      return slideContent.substring(0, nextIndex);
    }
  }

  String getNextContent() {
    return slideContent.substring(
      nextIndex,
    );
  }

  final slide = SlideData(
      // +1 because slide has not been added to list yet
      name: 'slide_${slides.length + 1}',
      options: SlideOptions.fromYamlMap(slideOptions),
      content: getCurrentContent(),
      snippets: snippetParser(getCurrentContent()));

  slides.add(slide);

  if (lastSlide) {
    return slides;
  }

  return findNextFrontMatterSlide(
    getNextContent(),
    slides: slides,
  );
}