Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom header id being overwritten #177

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

longtn-imt
Copy link

@longtn-imt longtn-imt commented Jan 3, 2025

When my doc gen-id use utf-8 it errors.

Example code with bug

import 'package:markdown/markdown.dart';
import 'package:static_shock/static_shock.dart';

Future<void> main(List<String> arguments) async {
  // Configure the static website generator.
  final StaticShock staticShock = StaticShock()
    // Here, you can directly hook into the StaticShock pipeline. For example,
    // you can copy an "images" directory from the source set to build set:
    ..pick(DirectoryPicker.parse("images"))
    // All 3rd party behavior is added through plugins, even the behavior
    // shipped with Static Shock.
    ..plugin(MarkdownPlugin(
      renderOptions: MarkdownRenderOptions(
        blockSyntaxes: [
          MyHeaderWithIdSyntax(),
        ],
      ),
    ))
    ..plugin(const JinjaPlugin())
    ..plugin(const PrettyUrlsPlugin())
    ..plugin(const RedirectsPlugin())
    ..plugin(const SassPlugin())
    ..plugin(DraftingPlugin(
      showDrafts: arguments.contains("preview"),
    ));

  // Generate the static website.
  await staticShock.generateSite();
}

class MyHeaderWithIdSyntax extends HeaderWithIdSyntax {
  @override
  Node parse(BlockParser parser) {
    final element = super.parse(parser) as Element;

    element.generatedId = element.children?.firstOrNull?.textContent
        .toLowerCase()
        .trim()
        .replaceAll(RegExp(r'\s'), '-')
        .replaceAll(RegExp(r'[^\p{L}\p{N}\p{P}]', unicode: true), '');

    return element;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant