Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
import java.nio.file.Path;

public class LatexInlinedCodePlugin implements InlinedCodePlugin {
private String latex;
public static final String ID = "latex";
public static final String SRC_KEY = "src";

@Override
public String id() {
return "latex";
return ID;
}

@Override
Expand All @@ -39,7 +40,7 @@ public InlinedCodePlugin create() {

@Override
public PluginResult process(ComponentsRegistry componentsRegistry, Path markupPath, PluginParams pluginParams) {
latex = pluginParams.getOpts().getRequiredString("src");
String latex = pluginParams.getOpts().getRequiredString(SRC_KEY);
return PluginResult.docElement(new DocElement("InlinedLatex", "latex", latex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.testingisdocumenting.znai.parser.commonmark.include.IncludeBlockParser;
import org.commonmark.parser.Parser.Builder;
import org.commonmark.parser.Parser.ParserExtension;
import org.testingisdocumenting.znai.parser.commonmark.include.LatexDollarInlineParser;

public class CommonMarkExtension implements ParserExtension {
private final PluginParamsFactory pluginParamsFactory;
Expand All @@ -30,6 +31,7 @@ public CommonMarkExtension(PluginParamsFactory pluginParamsFactory) {

@Override
public void extend(final Builder parserBuilder) {
parserBuilder.customBlockParserFactory(new IncludeBlockParser.Factory(pluginParamsFactory));
parserBuilder.customBlockParserFactory(new IncludeBlockParser.Factory(pluginParamsFactory))
.customInlineContentParserFactory(new LatexDollarInlineParser.Factory()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.testingisdocumenting.znai.extensions.fence.FencePlugin;
import org.testingisdocumenting.znai.extensions.include.IncludePlugin;
import org.testingisdocumenting.znai.extensions.inlinedcode.InlinedCodePlugin;
import org.testingisdocumenting.znai.extensions.latex.LatexInlinedCodePlugin;
import org.testingisdocumenting.znai.parser.HeadingProps;
import org.testingisdocumenting.znai.parser.ParserHandler;
import org.testingisdocumenting.znai.parser.commonmark.include.IncludeBlock;
import org.testingisdocumenting.znai.parser.commonmark.include.LatexDollarInline;
import org.testingisdocumenting.znai.parser.table.GfmTableToTableConverter;
import org.testingisdocumenting.znai.reference.DocReferences;
import org.commonmark.ext.front.matter.YamlFrontMatterBlock;
Expand Down Expand Up @@ -167,6 +169,10 @@ public void visit(CustomNode customNode) {
parserHandler.onStrikeThroughStart();
visitChildren(customNode);
parserHandler.onStrikeThroughEnd();
} else if (customNode instanceof LatexDollarInline dollarInline) {
handleInlineCodePlugin(componentsRegistry.pluginParamsFactory().create(LatexInlinedCodePlugin.ID,
"",
Collections.singletonMap(LatexInlinedCodePlugin.SRC_KEY, dollarInline.getLiteral())));
} else {
super.visit(customNode);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 znai maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.znai.parser.commonmark.include;

import org.commonmark.node.CustomNode;

public class LatexDollarInline extends CustomNode {
private final String literal;

public LatexDollarInline(String literal) {
this.literal = literal;
}

public String getLiteral() {
return literal;
}

@Override
public String toString() {
return "LatexDollarInline{" +
"literal='" + literal + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2024 znai maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.znai.parser.commonmark.include;

import org.commonmark.parser.beta.InlineContentParser;
import org.commonmark.parser.beta.InlineContentParserFactory;
import org.commonmark.parser.beta.InlineParserState;
import org.commonmark.parser.beta.ParsedInline;

import java.util.Set;

public class LatexDollarInlineParser implements InlineContentParser {
@Override
public ParsedInline tryParse(InlineParserState inlineParserState) {
var scanner = inlineParserState.scanner();
scanner.next();
var pos = scanner.position();

var end = scanner.find('$');
if (end == -1) {
return ParsedInline.none();
}

var content = scanner.getSource(pos, scanner.position()).getContent();
scanner.next();
return ParsedInline.of(new LatexDollarInline(content), scanner.position());
}

static public class Factory implements InlineContentParserFactory {
@Override
public Set<Character> getTriggerCharacters() {
return Set.of('$');
}

@Override
public InlineContentParser create() {
return new LatexDollarInlineParser();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@

org.testingisdocumenting.znai.extensions.file.FileInlinedCodePlugin
org.testingisdocumenting.znai.extensions.inlinedcode.IdentifierInlinedCodePlugin
org.testingisdocumenting.znai.extensions.textbadge.TextBadgeInlinedCodePlugin
org.testingisdocumenting.znai.extensions.textbadge.TextBadgeInlinedCodePlugin
org.testingisdocumenting.znai.extensions.latex.LatexInlinedCodePlugin
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.testingisdocumenting.znai.parser

import org.testingisdocumenting.webtau.WebTauCore
import org.testingisdocumenting.znai.parser.commonmark.MarkdownParser
import org.junit.Test

Expand Down Expand Up @@ -444,6 +445,17 @@ world""")
"process error\n")
}

@Test
void "inline latext formulas with dollar"() {
parse('hello $a=2$ world')
content.should == [
[ "type": "Paragraph", "content": [
["text": "hello ", "type": "SimpleText"],
["latex": "a=2", "type": "InlinedLatex"],
["text": " world", "type": "SimpleText"]
] ] ]
}

@Test
void "custom page data"() {
parse("---\ntitle: custom title\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: [Latex Math](snippets/math) inline `$` shortcut support
6 changes: 6 additions & 0 deletions znai-docs/znai/snippets/math.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ The result will be a following math expression.

It holds that `:latex: {src: "\\frac{1}{2} < \\sqrt{2}"}`.

Alternatively you can surround latex expression with `$`.

It holds that $\frac{1}{2} < \sqrt{2}$

It holds that $\frac{1}{2} < \sqrt{2}$

# Presentation Mode

In presentation mode, rendered expressions will automatically scale to make use of the screen space.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@

org.testingisdocumenting.znai.extensions.icons.IconInlinedCodePlugin
org.testingisdocumenting.znai.extensions.keyboard.KeyboardShortcutInlinedCodePlugin
org.testingisdocumenting.znai.extensions.latex.LatexInlinedCodePlugin
org.testingisdocumenting.znai.extensions.markup.MarkdownInlinedCodePlugin