Skip to content
Open
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
26 changes: 24 additions & 2 deletions src/mistune/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ def create_markdown(
hard_wrap: bool = False,
renderer: Optional[RendererRef] = "html",
plugins: Optional[Iterable[PluginRef]] = None,
enable_interactive: bool = False,
inject_assets: bool = False,
) -> Markdown:
"""Create a Markdown instance based on the given condition.

:param escape: Boolean. If using html renderer, escape html.
:param hard_wrap: Boolean. Break every new line into ``<br>``.
:param renderer: renderer instance, default is HTMLRenderer.
:param plugins: List of plugins.
:param enable_interactive: Enable interactive features (tabs, enhanced containers).
:param inject_assets: Inject frontend JS/CSS assets for interactive features.

This method is used when you want to re-use a Markdown instance::

Expand All @@ -46,13 +50,31 @@ def create_markdown(
# explicit and more similar to 2.x's API
renderer = None
elif renderer == "html":
renderer = HTMLRenderer(escape=escape)
renderer = HTMLRenderer(escape=escape, enable_interactive=enable_interactive)

inline = InlineParser(hard_wrap=hard_wrap)
real_plugins: Optional[Iterable[Plugin]] = None
if plugins is not None:
real_plugins = [import_plugin(n) for n in plugins]
return Markdown(renderer=renderer, inline=inline, plugins=real_plugins)

# Add container directive plugins if interactive mode is enabled
if enable_interactive:
from .directives import ContainerDirective, TabsDirective, TipDirective
from .plugins.code_cluster import code_cluster_plugin
if real_plugins is None:
real_plugins = []
else:
real_plugins = list(real_plugins)
real_plugins.append(ContainerDirective([TabsDirective(), TipDirective()]))
real_plugins.append(code_cluster_plugin)

return Markdown(
renderer=renderer,
inline=inline,
plugins=real_plugins,
enable_interactive=enable_interactive,
inject_assets=inject_assets,
)


html: Markdown = create_markdown(escape=False, plugins=["strikethrough", "footnotes", "table", "speedup"])
Expand Down
18 changes: 18 additions & 0 deletions src/mistune/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ def _md(args: argparse.Namespace) -> "Markdown":
renderer = MarkdownRenderer()
else:
renderer = args.renderer

# Add container directive plugins if interactive mode is enabled
if args.interactive:
from .directives import ContainerDirective, TabsDirective, TipDirective
plugins.append(ContainerDirective([TabsDirective(), TipDirective()]))

return create_markdown(
escape=args.escape,
hard_wrap=args.hardwrap,
renderer=renderer,
plugins=plugins,
enable_interactive=args.interactive,
inject_assets=args.inject_assets,
)


Expand Down Expand Up @@ -101,6 +109,16 @@ def cli() -> None:
default="html",
help="specify the output renderer",
)
parser.add_argument(
"--interactive",
action="store_true",
help="enable interactive features (tabs, enhanced containers)",
)
parser.add_argument(
"--inject-assets",
action="store_true",
help="inject frontend JS/CSS assets for interactive features",
)
parser.add_argument("--version", action="version", version="mistune " + version)
args = parser.parse_args()

Expand Down
Loading