taufik-nurrohman / parsedown-extra-plugin

Configurable Markdown to HTML converter with Parsedown Extra.
MIT License
62 stars 13 forks source link

When used with a foreach. Only the first result has a rel=nofollow #13

Closed idmx1 closed 5 years ago

idmx1 commented 5 years ago

I am using Parsedown to parse a text with links from MySQL.

As recommended in another answer on StackOverflow: https://stackoverflow.com/a/47148927/10415318 I am using the Parsedown Extra Plugin https://github.com/taufik-nurrohman/parsedown-extra-plugin

When doing the query and the foreach all I get is the first result to have a rel=ugc attribute, the other ones don't have it. The links render ok, just not the rel attribute.

Here is the PHP code I have:

 public function getSingleThread($url) {
            $Parsedown = new ParsedownExtraPlugin();

            $Parsedown->linkAttributes = function($Text, $Attributes, &$Element, $Internal) {
              if (!$Internal) {
                  return [
                      'rel' => 'ugc',
                      'target' => '_blank'
                  ];
              }
              return [];
            };

            $thread = lib::$db->GetRow("SELECT t.*,
                                         t.description AS text,
                                         p.photo_small,
                                         p.username,
                                         p.name
                                      FROM forum_threads AS t
                                      INNER JOIN user_profiles AS p
                                        ON t.id_author = p.id_user
                                      AND t.url = ".lib::$db->qstr($url));

            $userId = $_SESSION["user"]["id_user"];

            $thread['editDesc'] = $thread['description'];

            $thread['description'] = $Parsedown->text($thread['description']);

            $idThread = $thread['id_thread'];
            $posts = lib::$db->GetAll("SELECT
                                        r.*,
                                        p.photo_small,
                                        p.username,
                                        p.name AS author_name,
                                        p.occupation AS author_job
                                      FROM forum_replies AS r
                                      INNER JOIN user_profiles as p
                                          ON r.id_author = p.id_user AND r.id_parent = 0 AND id_thread =".lib::$db->qstr($idThread)."
                                      ORDER BY date_added");

            foreach ($posts as &$postItem) {
              $postItem["replies"] = lib::$db->GetAll("SELECT r.*, p.photo_small,
                                                            p.username,
                                                            p.name AS author_name,
                                                            p.occupation AS author_job
                                                          FROM forum_replies AS r
                                                          INNER JOIN user_profiles as p
                                                              ON r.id_author = p.id_user AND r.id_parent = ".lib::$db->qstr($postItem["id_reply"])."
                                                          ORDER BY date_added");

                $old = $postItem['description'];
                $postItem['description'] = $Parsedown->text($old);
                $postItem['editDesc'] = $old;

                foreach ($postItem["replies"] as &$replies) {
                    $oldDesc = $replies['description'];
                    $replies['description'] = $Parsedown->text($replies['description']);
                    $replies['editDesc'] = $oldDesc;

                }
            }

            lib::$view->assign("thread",$thread);
            lib::$view->assign("posts", $posts);
            lib::$view->assign("userId",$userId);
            lib::$view->assign("pageTemplate", "front/forum/forum_thread.tpl");
        }
taufik-nurrohman commented 5 years ago

Making class instance within the for-each loop will increase the memory used. But what happens when you do this?

foreach ($posts as &$postItem) {
    $Parsedown = new ParsedownExtraPlugin;
    // ... your code goes here
}
idmx1 commented 5 years ago

Hey, thanks for answering.

Just tried it but its the same result.

Updated my code to give you a full picture of what I am doing.

idmx1 commented 5 years ago

Actually based on your answer, this works:

foreach ($posts as &$postItem) {
                $Parsedown->linkAttributes = function($Text, $Attributes, &$Element, $Internal) {
              if (!$Internal) {
                  return [
                      'rel' => 'ugc',
                      'target' => '_blank'
                  ];
              }
              return [];
            };

// Code goes in here
}
taufik-nurrohman commented 5 years ago

This seems to be caused by inlineImage() method that inherits to inlineLink() method. In my extension, I need to alternately attach and unmount the linkAttributes property on the image and link. I will mark this issue as a bug.

For quick fix, try to put this line next out of the if ( ... ) {} statement.