Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Nextcloud
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TeDomum
Nextcloud
Commits
4c81f5c4
Unverified
Commit
4c81f5c4
authored
4 years ago
by
Morris Jobke
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Plain Diff
Merge pull request #25212 from nextcloud/enh/preview-markdown
Enhance markdown file preview rendering
parents
6129a851
01f01366
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/private/Preview/MarkDown.php
+110
-0
110 additions, 0 deletions
lib/private/Preview/MarkDown.php
with
110 additions
and
0 deletions
lib/private/Preview/MarkDown.php
+
110
−
0
View file @
4c81f5c4
...
...
@@ -24,6 +24,9 @@
namespace
OC\Preview
;
use
OCP\Files\File
;
use
OCP\IImage
;
class
MarkDown
extends
TXT
{
/**
* {@inheritDoc}
...
...
@@ -31,4 +34,111 @@ class MarkDown extends TXT {
public
function
getMimeType
():
string
{
return
'/text\/(x-)?markdown/'
;
}
public
function
getThumbnail
(
File
$file
,
int
$maxX
,
int
$maxY
):
?IImage
{
$content
=
$file
->
fopen
(
'r'
);
if
(
$content
===
false
)
{
return
null
;
}
$content
=
stream_get_contents
(
$content
,
3000
);
//don't create previews of empty text files
if
(
trim
(
$content
)
===
''
)
{
return
null
;
}
// Merge text paragraph lines that might belong together
$content
=
preg_replace
(
'/^(\s*)\*\s/mU'
,
'$1- '
,
$content
);
$content
=
preg_replace
(
'/((?!^(\s*-|#)).*)(\w|\\|\.)(\r\n|\n|\r)(\w|\*)/mU'
,
'$1 $3'
,
$content
);
// Remove markdown symbols that we cannot easily represent in rendered text in the preview
$content
=
preg_replace
(
'/\*\*(.*)\*\*/U'
,
'$1'
,
$content
);
$content
=
preg_replace
(
'/\*(.*)\*/U'
,
'$1'
,
$content
);
$content
=
preg_replace
(
'/\_\_(.*)\_\_/U'
,
'$1'
,
$content
);
$content
=
preg_replace
(
'/\_(.*)\_/U'
,
'$1'
,
$content
);
$content
=
preg_replace
(
'/\~\~(.*)\~\~/U'
,
'$1'
,
$content
);
$content
=
preg_replace
(
'/\!?\[((.|\n)*)\]\((.*)\)/mU'
,
'$1 ($3)'
,
$content
);
$content
=
preg_replace
(
'/\n\n+/'
,
"
\n
"
,
$content
);
$content
=
preg_replace
(
'/[\x{10000}-\x{10FFFF}]/u'
,
''
,
$content
);
$lines
=
preg_split
(
"/
\r\n
|
\n
|
\r
/"
,
$content
);
// Define text size of text file preview
$fontSize
=
$maxX
?
(
int
)
((
1
/
(
$maxX
>=
512
?
60
:
40
)
*
$maxX
))
:
10
;
$image
=
imagecreate
(
$maxX
,
$maxY
);
imagecolorallocate
(
$image
,
255
,
255
,
255
);
$textColor
=
imagecolorallocate
(
$image
,
0
,
0
,
0
);
$fontFile
=
__DIR__
.
'/../../../core/fonts/NotoSans-Regular.ttf'
;
$fontFileBold
=
__DIR__
.
'/../../../core/fonts/NotoSans-Bold.ttf'
;
$canUseTTF
=
function_exists
(
'imagettftext'
);
$textOffset
=
(
int
)
min
(
$maxX
*
0.05
,
$maxY
*
0.05
);
$nextLineStart
=
0
;
$y
=
$textOffset
;
foreach
(
$lines
as
$line
)
{
$actualFontSize
=
$fontSize
;
if
(
mb_strpos
(
$line
,
'# '
)
===
0
)
{
$actualFontSize
*=
2
;
}
if
(
mb_strpos
(
$line
,
'## '
)
===
0
)
{
$actualFontSize
*=
1.8
;
}
if
(
mb_strpos
(
$line
,
'### '
)
===
0
)
{
$actualFontSize
*=
1.6
;
}
if
(
mb_strpos
(
$line
,
'#### '
)
===
0
)
{
$actualFontSize
*=
1.4
;
}
if
(
mb_strpos
(
$line
,
'##### '
)
===
0
)
{
$actualFontSize
*=
1.2
;
}
if
(
mb_strpos
(
$line
,
'###### '
)
===
0
)
{
$actualFontSize
*=
1.1
;
}
// Add spacing before headlines
if
(
$actualFontSize
!==
$fontSize
&&
$y
!==
$textOffset
)
{
$y
+=
(
int
)(
$actualFontSize
*
2
);
}
$x
=
$textOffset
;
$y
+=
(
int
)(
$nextLineStart
+
$actualFontSize
);
if
(
$canUseTTF
===
true
)
{
$wordWrap
=
(
int
)((
1
/
$actualFontSize
*
1.3
)
*
$maxX
);
// Get rid of markdown symbols that we still needed for the font size
$line
=
preg_replace
(
'/^#*\s/'
,
''
,
$line
);
$wrappedText
=
wordwrap
(
$line
,
$wordWrap
,
"
\n
"
);
$linesWrapped
=
count
(
explode
(
"
\n
"
,
$wrappedText
));
imagettftext
(
$image
,
$actualFontSize
,
0
,
$x
,
$y
,
$textColor
,
$actualFontSize
===
$fontSize
?
$fontFile
:
$fontFileBold
,
$wrappedText
);
$nextLineStart
=
(
int
)(
$linesWrapped
*
ceil
(
$actualFontSize
*
2
));
if
(
$actualFontSize
!==
$fontSize
&&
$y
!==
$textOffset
)
{
$nextLineStart
-=
$actualFontSize
;
}
}
else
{
$y
-=
(
int
)
$fontSize
;
imagestring
(
$image
,
1
,
$x
,
$y
,
$line
,
$textColor
);
$nextLineStart
=
$fontSize
;
}
if
(
$y
>=
$maxY
)
{
break
;
}
}
$imageObject
=
new
\OC_Image
();
$imageObject
->
setResource
(
$image
);
return
$imageObject
->
valid
()
?
$imageObject
:
null
;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment