Skip to content

Commit

Permalink
Jump to correct heading via id
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed May 25, 2024
1 parent a2afb69 commit db2581e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@
import com.vladsch.flexmark.ext.yaml.front.matter.AbstractYamlFrontMatterVisitor;
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.html.renderer.HeaderIdGenerator;
import com.vladsch.flexmark.html.renderer.LinkResolverContext;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.superscript.SuperscriptExtension;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.builder.Extension;
import com.vladsch.flexmark.util.html.Attributes;
import com.vladsch.flexmark.util.options.MutableDataHolder;
import com.vladsch.flexmark.util.options.MutableDataSet;
import com.vladsch.flexmark.html.AttributeProvider;
import com.vladsch.flexmark.html.AttributeProviderFactory;
import com.vladsch.flexmark.html.renderer.AttributablePart;

import net.gsantner.markor.R;
import net.gsantner.markor.format.TextConverterBase;
Expand All @@ -49,6 +55,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -149,7 +156,8 @@ public class MarkdownTextConverter extends TextConverterBase {
TypographicExtension.create(), // https://github.com/vsch/flexmark-java/wiki/Typographic-Extension
GitLabExtension.create(), // https://github.com/vsch/flexmark-java/wiki/Extensions#gitlab-flavoured-markdown
AdmonitionExtension.create(), // https://github.com/vsch/flexmark-java/wiki/Extensions#admonition
FootnoteExtension.create() // https://github.com/vsch/flexmark-java/wiki/Footnotes-Extension#overview
FootnoteExtension.create(), // https://github.com/vsch/flexmark-java/wiki/Footnotes-Extension#overview
LineNumberIdExtension.create()
);
public static final Parser flexmarkParser = Parser.builder().extensions(flexmarkExtensions).build();
public static final HtmlRenderer flexmarkRenderer = HtmlRenderer.builder().extensions(flexmarkExtensions).build();
Expand All @@ -171,6 +179,8 @@ public String convertMarkup(String markup, Context context, boolean lightMode, b

options.set(Parser.EXTENSIONS, flexmarkExtensions);

options.set(Parser.TRACK_DOCUMENT_LINES, true);

options.set(Parser.SPACE_IN_LINK_URLS, true); // Allow links like [this](some filename with spaces.md)

// options.set(HtmlRenderer.SOFT_BREAK, "<br />\n"); // Add linefeed to HTML break
Expand Down Expand Up @@ -331,17 +341,13 @@ public String convertMarkup(String markup, Context context, boolean lightMode, b

if (enableLineNumbers) {
// For Prism line numbers plugin
onLoadJs += "enableLineNumbers();adjustLineNumbers();";
onLoadJs += "enableLineNumbers(); adjustLineNumbers();";
}

// Deliver result
return putContentIntoTemplate(context, converted, lightMode, file, onLoadJs, head);
}

public static String generateHeaderId(String headerText) {
return HeaderIdGenerator.generateId(headerText, toDashChars, false, false);
}

private String escapeSpacesInLink(final String markup) {
final Matcher matcher = linkPattern.matcher(markup);
if (!matcher.find()) {
Expand Down Expand Up @@ -440,4 +446,58 @@ private String replaceTokens(final String markup, final Map<String, List<String>

return markupReplaced;
}

private static class LineNumberIdProvider implements AttributeProvider {
@Override
public void setAttributes(Node node, AttributablePart part, Attributes attributes) {
if (node instanceof com.vladsch.flexmark.ast.Heading) {
final Document document = node.getDocument();
final int lineNumber = document.getLineNumber(node.getStartOffset());
attributes.addValue("id", "line-" + lineNumber);
}
}

public static AttributeProviderFactory Factory() {
return new LineNumberIdProviderFactory();
}
}

private static class LineNumberIdProviderFactory implements AttributeProviderFactory {

@Override
public Set<Class<? extends AttributeProviderFactory>> getAfterDependents() {
return null;
}

@Override
public Set<Class<? extends AttributeProviderFactory>> getBeforeDependents() {
return null;
}

@Override
public boolean affectsGlobalScope() {
return false;
}

@Override
public AttributeProvider create(LinkResolverContext context) {
return new LineNumberIdProvider();
}
}

private static class LineNumberIdExtension implements HtmlRenderer.HtmlRendererExtension {
@Override
public void rendererOptions(MutableDataHolder options) {

}

@Override
public void extend(HtmlRenderer.Builder rendererBuilder, String rendererType) {
rendererBuilder.attributeProviderFactory(LineNumberIdProvider.Factory());
}

public static HtmlRenderer.HtmlRendererExtension create() {
return new LineNumberIdExtension();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -840,12 +840,10 @@ public static void showHeadlineDialog(

dopt.positionCallback = result -> {
final int index = filtered.get(result.get(0));
TextViewUtils.selectLines(edit, headings.get(index).line);

final String header = headings.get(index).str;
final String headerText = header.substring(header.lastIndexOf('#') + 1).trim();
final String id = MarkdownTextConverter.generateHeaderId(headerText);
webView.loadUrl("javascript:document.getElementById('" + id + "').scrollIntoView();");
final int line = headings.get(index).line;
TextViewUtils.selectLines(edit, line);
final String jumpJs = "javascript:document.getElementById('line-" + line + "').scrollIntoView();";
webView.loadUrl(jumpJs);
};

dopt.neutralButtonText = R.string.filter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ private void stopBlinking() {
}

private void loadFolder(final File folder) {
stopBlinking();
executorService.execute(() -> {
synchronized (LOAD_FOLDER_SYNC_OBJECT) {

Expand Down Expand Up @@ -728,6 +727,7 @@ private void loadFolder(final File folder) {
// Must be called from UI thread
// TODO - add logic to notify the changed bits
notifyDataSetChanged();
stopBlinking();

if (prevFolder != null && _currentFolder.equals(prevFolder.getParentFile())) {
doAfterChange(() -> showAndFlash(prevFolder));
Expand Down

0 comments on commit db2581e

Please sign in to comment.