dokuwiki-plugin-prosemirror icon indicating copy to clipboard operation
dokuwiki-plugin-prosemirror copied to clipboard

Conflict with "Numbered Headings Plugin"

Open Juergen-aus-Zuendorf opened this issue 5 years ago • 8 comments

Hi,

the prosemirror plugin converts the dynamic headline numbering of the "Numbered Headings Plugin" into fix numbers

So

====== - Level 1 Headline ======
===== - Level 2 Headline =====
==== -#5 Level 3 Headline ====
==== - Level 3 Headline ====
===== -#7 Level 2 Headline =====
==== - Level 3 Headline ====

is converted to

====== 1. Level 1 Headline ======

===== 1.1 Level 2 Headline =====

==== 1.1.5 Level 3 Headline ====

==== 1.1.6 Level 3 Headline ====

===== 1.7 Level 2 Headline =====

==== 1.7.1 Level 3 Headline ====

Regards Juergen

Juergen-aus-Zuendorf avatar Feb 25 '20 14:02 Juergen-aus-Zuendorf

Thank you for opening this issue. CosmoCode is a software company in Berlin providing services for wiki, app and web development. As such we can't guarantee quick responses for issues opened on our Open Source projects. If you require certain features or bugs fixed, you can always hire us. Feel free to contact us at [email protected] for an offer.

auto-comment[bot] avatar Feb 25 '20 14:02 auto-comment[bot]

Hi. We have exactly the same problem and it is a very topic for us! How much does it cost to fix this bug?

jankokert avatar Jan 11 '22 12:01 jankokert

Hi. I took a closer look at this file:

dokuwiki-plugin-prosemirror/parser/HeadingNode.php

The problem is, that the $this->text variable is not checked for digits at the front. I added a few lines of code and now it works fine: Here is the complete code:

<?php
/**
 * Created by IntelliJ IDEA.
 * User: michael
 * Date: 7/7/17
 * Time: 4:49 PM
 */

namespace dokuwiki\plugin\prosemirror\parser;

class HeadingNode extends Node
{

    protected $parent;
    protected $level;
    protected $text;
    protected $is_numbered;    

    public function __construct($data, Node $parent)
    {
        if (trim($data['content'][0]['text']) === '') {
            return;
        }

        $this->parent = &$parent;
        $this->level = $data['attrs']['level'];
        //$this->text = $data['content'][0]['text'];  // orginal line

        $subject = $data['content'][0]['text'];
        $pattern = '/([\d\.]+)\s(.+)/';  // 1st group: digits;  2nd group: title
        $found = preg_match($pattern, $subject, $matches);
        if ($found == 1) {
            $this->is_numbered = true;
            $this->text = $matches[2];
        } else {
            $this->is_numbered = false;
            $this->text = $subject;
        }
    }

    public function toSyntax()
    {
        $wrapper = [
            1 => '======',
            2 => '=====',
            3 => '====',
            4 => '===',
            5 => '==',
        ];

        if ($this->is_numbered){
            return $wrapper[$this->level] . ' - ' . $this->text . ' ' . $wrapper[$this->level];
        } else {
            return $wrapper[$this->level] . ' ' . $this->text . ' ' . $wrapper[$this->level];
        }
    }
}

Cheers!

jankokert avatar Jan 11 '22 14:01 jankokert

False-positive are possible, in case people use an manually typed number at the start of the heading, especially if you do not use the plugin. I think the unexpected effects can be reduced by applying this only if the Numbered heading plugin is installed?

Klap-in avatar Jan 12 '22 00:01 Klap-in

Ok, good point. How can we check, that the numbered-headings plugin is installed?

The original problem is, that the parser gets the digits (1.2.3 text) and not the dash (- text) of the heading. Do we have access to the original source element, before numbered headings do its job?

I also optimized the regex to minimize false positives:

$pattern = '/(^\d\d?\.(\d\d?)?(\.\d\d?)?)\s(?<text>.+)/';
...
$this->text = $matches['text'];

jankokert avatar Jan 12 '22 08:01 jankokert

You can use plugin_isdisabled('pluginbasename'). See for more info https://www.dokuwiki.org/devel:common_plugin_functions#load_other_plugins

Klap-in avatar Jan 12 '22 08:01 Klap-in

@splitbrain Is this actually something that should be implemented via the Numbered heading plugin? Looking at the gallery plugin implementing does not yet look obvious..

Klap-in avatar Jan 12 '22 08:01 Klap-in

This should definitely be implemented in the numbered heading plugin. If it's possible to do so already without changes in prosemiror I can't tell without looking at it in detail.

The approach above is wrong for sure. It is not prosemirror's task to reverse plugin rendered HTML. Instead the plugin needs to provide the appropriate hints to the editor.

@jankokert send an email to address mentioned above if you want an offer

splitbrain avatar Jan 12 '22 09:01 splitbrain