@@ -58,6 +58,7 @@ export interface TransformMarkdownOptions {
58
58
headingIdGenerator ?: HeadingIdGenerator ;
59
59
notebook : Notebook ;
60
60
forJest ?: boolean ;
61
+ timestamp ?: number ;
61
62
}
62
63
63
64
const fileExtensionToLanguageMap = {
@@ -245,6 +246,7 @@ export async function transformMarkdown(
245
246
notebook,
246
247
forJest = false ,
247
248
fileHash,
249
+ timestamp,
248
250
} : TransformMarkdownOptions ,
249
251
) : Promise < TransformMarkdownOutput > {
250
252
// Replace CRLF with LF
@@ -593,7 +595,7 @@ export async function transformMarkdown(
593
595
// =========== Start: File import ============
594
596
const importMatch = line . match ( / ^ ( \s * ) @ i m p o r t ( \s + ) " ( [ ^ " ] + ) " ; ? / ) ;
595
597
const imageImportMatch = line . match (
596
- / ^ ( \s * ) ! \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] + ) \) (?: { ( [ ^ } ] * ) } ) ? \s * $ / ,
598
+ / ^ ( \s * ) ! \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] + ) \) (?: { ( [ ^ } ] * ) } ) ? ( \s * ) $ / ,
597
599
) ;
598
600
const wikilinkImportMatch = line . match (
599
601
/ ^ ( \s * ) ! \[ \[ ( .+ ?) \] \] (?: { ( [ ^ } ] * ) } ) ? \s * $ / ,
@@ -626,6 +628,9 @@ export async function transformMarkdown(
626
628
}
627
629
}
628
630
}
631
+ if ( canCreateAnchor ( ) ) {
632
+ config [ 'data-source-line' ] = lineNo + 1 ;
633
+ }
629
634
630
635
let absoluteFilePath : string ;
631
636
if (
@@ -648,14 +653,12 @@ export async function transformMarkdown(
648
653
const extname = path . extname ( absoluteFilePath ) . toLocaleLowerCase ( ) ;
649
654
let output = '' ;
650
655
if ( filePath === '[TOC]' ) {
651
- if ( ! config ) {
652
- config = {
653
- // same case as in normalized attributes
654
- [ 'depth_from' ] : 1 ,
655
- [ 'depth_to' ] : 6 ,
656
- [ 'ordered_list' ] : true ,
657
- } ;
658
- }
656
+ /*
657
+ // NOTE: No need to set this
658
+ config['depth_from'] = config['depth_from'] ?? 1;
659
+ config['depth_to'] = config['depth_to'] ?? 6;
660
+ config['ordered_list'] = config['ordered_list'] ?? true;
661
+ */
659
662
config [ 'cmd' ] = 'toc' ;
660
663
config [ 'hide' ] = true ;
661
664
config [ 'run_on_save' ] = true ;
@@ -693,51 +696,42 @@ export async function transformMarkdown(
693
696
imageSrc =
694
697
path . relative ( fileDirectoryPath , absoluteFilePath ) +
695
698
'?' +
696
- Math . random ( ) ;
699
+ ( timestamp ?? Math . random ( ) ) ;
697
700
} else {
698
701
imageSrc =
699
702
'/' +
700
703
path . relative ( projectDirectoryPath , absoluteFilePath ) +
701
704
'?' +
702
- Math . random ( ) ;
705
+ ( timestamp ?? Math . random ( ) ) ;
703
706
}
704
707
// enchodeURI(imageSrc) is wrong. It will cause issue on Windows
705
708
// #414: https://github.com/shd101wyy/markdown-preview-enhanced/issues/414
706
709
imageSrc = imageSrc . replace ( / / g, '%20' ) . replace ( / \\ / g, '/' ) ;
707
710
filesCache [ filePath ] = imageSrc ;
708
711
}
709
712
710
- if ( config ) {
711
- if (
712
- config [ 'width' ] ||
713
- config [ 'height' ] ||
714
- config [ 'class' ] ||
715
- config [ 'id' ]
716
- ) {
717
- output = `<img src="${ imageSrc } " ` ;
718
- for ( const key in config ) {
719
- // eslint-disable-next-line no-prototype-builtins
720
- if ( config . hasOwnProperty ( key ) ) {
721
- output += ` ${ key } ="${ config [ key ] } " ` ;
722
- }
723
- }
724
- output += '>' ;
725
- } else {
726
- output = '![' ;
727
- if ( config [ 'alt' ] ) {
728
- output += config [ 'alt' ] ;
729
- }
730
- output += `](${ imageSrc } ` ;
731
- if ( config [ 'title' ] ) {
732
- output += ` "${ config [ 'title' ] } "` ;
733
- }
734
- output += ') ' ;
735
- }
713
+ output = '![' ;
714
+ if ( config [ 'alt' ] ) {
715
+ output += config [ 'alt' ] ;
716
+ delete config [ 'alt' ] ;
717
+ }
718
+ output += `](${ imageSrc } ` ;
719
+ if ( config [ 'title' ] ) {
720
+ output += ` "${ config [ 'title' ] } "` ;
721
+ delete config [ 'title' ] ;
722
+ }
723
+ output += ')' ;
724
+ const configStr = stringifyBlockAttributes ( config ) ;
725
+ if ( configStr ) {
726
+ output += `{${ configStr } } ` ;
736
727
} else {
737
- output = ` ` ;
728
+ output += ' ' ;
738
729
}
739
730
} else if ( imageImportMatch ) {
740
- output = imageImportMatch [ 0 ] ; // NOTE: Don't change anything here
731
+ const configStr = stringifyBlockAttributes ( config ) ;
732
+ output = `![${ imageImportMatch [ 2 ] ?? '' } ](${
733
+ imageImportMatch [ 3 ] ?? ''
734
+ } )${ configStr ? `{${ configStr } }` : '' } ${ imageImportMatch [ 5 ] } `;
741
735
}
742
736
i = end + 1 ;
743
737
lineNo = lineNo + 1 ;
@@ -756,7 +750,7 @@ export async function transformMarkdown(
756
750
) ;
757
751
filesCache [ absoluteFilePath ] = fileContent ;
758
752
759
- if ( config && ( config [ 'line_begin' ] || config [ 'line_end' ] ) ) {
753
+ if ( config [ 'line_begin' ] || config [ 'line_end' ] ) {
760
754
const lines = fileContent . split ( / \n / ) ;
761
755
fileContent = lines
762
756
. slice (
@@ -766,7 +760,7 @@ export async function transformMarkdown(
766
760
. join ( '\n' ) ;
767
761
}
768
762
769
- if ( config && config [ 'code_block' ] ) {
763
+ if ( config [ 'code_block' ] ) {
770
764
const fileExtension = extname . slice ( 1 , extname . length ) ;
771
765
output = `\`\`\`${
772
766
config [ 'as' ] ||
@@ -775,7 +769,7 @@ export async function transformMarkdown(
775
769
} ${ stringifyBlockAttributes (
776
770
config ,
777
771
) } \n${ fileContent } \n\`\`\` `;
778
- } else if ( config && config [ 'cmd' ] ) {
772
+ } else if ( config [ 'cmd' ] ) {
779
773
if ( ! config [ 'id' ] ) {
780
774
// create `id` for code chunk
781
775
config [ 'id' ] = computeChecksum ( absoluteFilePath ) ;
@@ -880,7 +874,7 @@ export async function transformMarkdown(
880
874
// css or less file
881
875
output = `<style>${ fileContent } </style>` ;
882
876
} else if ( extname === '.pdf' ) {
883
- if ( config && config [ 'page_no' ] ) {
877
+ if ( config [ 'page_no' ] ) {
884
878
// only disply the nth page. 1-indexed
885
879
const pages = fileContent . split ( '\n' ) ;
886
880
let pageNo = parseInt ( config [ 'page_no' ] , 10 ) - 1 ;
@@ -943,11 +937,8 @@ export async function transformMarkdown(
943
937
output = "<script>${fileContent}</script>"
944
938
*/
945
939
// # codeblock
946
- let aS = null ;
947
- if ( config ) {
948
- aS = config [ 'as' ] ;
949
- }
950
- if ( config && config [ 'code_block' ] === false ) {
940
+ const aS = config [ 'as' ] ?? null ;
941
+ if ( config [ 'code_block' ] === false ) {
951
942
// https://github.com/shd101wyy/markdown-preview-enhanced/issues/916
952
943
output = fileContent ;
953
944
} else {
@@ -1001,44 +992,6 @@ export async function transformMarkdown(
1001
992
// =========== End: File import ============
1002
993
// =========== Start: Normal line ============
1003
994
else {
1004
- // =========== Start: Add attributes to links and images ========
1005
- if ( canCreateAnchor ( ) ) {
1006
- let newLine = '' ;
1007
- let restLine = line ;
1008
- const regexp = / ! ? \[ ( [ ^ \] ] * ) \] \( ( [ ^ ) ] * ) \) / ;
1009
- // Add and data-source-line to links and images {...} attributes
1010
- // eslint-disable-next-line no-constant-condition
1011
- while ( true ) {
1012
- const match = restLine . match ( regexp ) ;
1013
- if ( ! match || typeof match . index !== 'number' ) {
1014
- newLine = newLine + restLine ;
1015
- break ;
1016
- } else {
1017
- newLine =
1018
- newLine + restLine . substring ( 0 , match . index + match [ 0 ] . length ) ;
1019
- restLine = restLine . substring ( match . index + match [ 0 ] . length ) ;
1020
-
1021
- if ( restLine [ 0 ] === '{' ) {
1022
- // Might find attribute
1023
- // TODO: Write a generic parser for this
1024
- const end = restLine . indexOf ( '}' ) ;
1025
- if ( end > 0 ) {
1026
- const attributeString = restLine . substring ( 1 , end ) ;
1027
- newLine += `{data-source-line="${
1028
- lineNo + 1
1029
- } " ${ attributeString } }`;
1030
- restLine = restLine . substring ( end + 1 ) ;
1031
- }
1032
- } else {
1033
- newLine += `{data-source-line="${ lineNo + 1 } "}` ;
1034
- }
1035
- }
1036
- }
1037
- line = newLine ;
1038
- }
1039
-
1040
- // =========== End: Add attributes to links and images ========
1041
-
1042
995
i = end + 1 ;
1043
996
lineNo = lineNo + 1 ;
1044
997
outputString = outputString + line + '\n' ;
0 commit comments